Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.

Similar presentations


Presentation on theme: "Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g."— Presentation transcript:

1 Pointer

2 lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g. x = 1.0; intarray[2] = 17; Constants and expressions are not lvalues

3 Memory and Data Representation Every lvalue is stored somewhere in memory and therefore has an address Once allocated, the address of an lvalue never changes, even though its contents may change Depending on type of data, different lvalues require different amounts of memory The address of an lvalue is itself data that can be manipulated and stored in memory

4 Pointer Declaration A pointer is a variable storing the address of another variable Declaration: type *ptr; Example: int *p1, *p2; p1 and p2 are referred to as pointers-to-int, and may contain the addresses of integers

5 Pointer Operations C++ defines two operators that manipulate pointer values: &address-of *value-pointed-to (dereference) Operand of & must be lvalue, and & returns the address of the lvalue Operand of * must be a pointer, and * returns the value pointed to by the pointer

6 Getting the Address of a Variable Each variable in program is stored at a unique address Use address operator & to get address of a variable: int num = -99; cout << &num; // prints address // in hexadecimal

7 Pointer Variables Definition: int *intptr; Read as: “ intptr can hold the address of an int” Spacing in definition does not matter: int * intptr; // same as above

8 Pointer Variables Assigning an address to a pointer variable: int *intptr; intptr = &num; Memory layout: numintptr 25 0x4a00 address of num : 0x4a00

9

10 The Indirection Operator The indirection operator ( * ) dereferences a pointer. It allows you to access the item that the pointer points to. int x = 25; int *intptr = &x; cout << *intptr << endl; This prints 25.

11

12

13 Special pointer NULL NULL is the value of a pointer that does not currently point to any data (usually value 0) Can not dereference a NULL pointer Exists for applications infile.open(”file.txt”); if (infile == NULL) printf(“File not open”);

14 Pointers as Function Parameters A pointer can be a parameter Works like reference variable to allow change to argument from within function Requires: 1) asterisk * on parameter in prototype and heading void getNum(int *ptr); // ptr is pointer to an int 2) asterisk * in body to dereference the pointer cin >> *ptr; 3) address as argument to the function getNum(&num); // pass address of num to getNum

15 Example void swap(int *x, int *y) {int temp; temp = *x; *x = *y; *y = temp; } int num1 = 2, num2 = -3; swap(&num1, &num2);

16 (Program Continues)

17

18 Another Example To modify value of argument, use pointer void SetToZero(int *ip) { *ip = 0; } which makes “ip” a pointer to the location of the value of the argument of the call SetToZero(&x);

19 Advantage of use of & operator When calling a function using func(x); you know the value of the variable x will not change, whereas calling a function using func(&x); is allowed to change the value of x Easier to predict effects of function call

20 SwapInteger function When sorting arrays, we wanted to use SwapIntegers(array[low], array[high]); Using pointers allows the following: void SwapIntegers(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; } and SwapIntegers(&array[low], &array[high]);

21 Returning Multiple Results Convert time (minutes) to hours and minutes void ConvertTime(int time, int *hours, int *mins) { *hours = time / 60; *mins = time % 60; } Call with ConvertTime(t, &h, &m);

22 Or Use Two functions int Hours(int time) { return (time / 60); } int Minutes(int time) { return (time % 60); }


Download ppt "Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g."

Similar presentations


Ads by Google