Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers CSE 5100 Data Structures and Algorithms.

Similar presentations


Presentation on theme: "Pointers CSE 5100 Data Structures and Algorithms."— Presentation transcript:

1 Pointers CSE 5100 Data Structures and Algorithms

2 Getting the address of a variable b You need to use the address operator & #include #include void main() { int num; int num; num = 22; num = 22; cout << "num= " << num << endl; cout << "num= " << num << endl; cout << "The address of num = " << &num << endl; cout << "The address of num = " << &num << endl;}

3 Storing addresses  We can store addresses in suitably declared variables num_addr = &num; b The variable num_addr is called a pointer variable b Pointers are simply variables that are used to store the addresses of other variables.

4 Storing addresses (cont.)

5 Declaring Pointers  You need to specify two things: (a) the data type of the variable pointed to by the pointer. (a) the data type of the variable pointed to by the pointer. (b) the dereferencing operator * followed by the name of the pointer. int *num_addr;

6 Dereferencing pointers  To obtain the contents of the variable pointed to by a pointer, we need to use the dereferencing operator *, followed by the name of the pointer. #include #include void main() { int *num_addr; int *num_addr; int miles, dist; int miles, dist; miles = 22; miles = 22; num_addr = &miles; num_addr = &miles; cout << "Address stored in num_addr is " << num_addr << endl; cout << "Address stored in num_addr is " << num_addr << endl; cout << "Value pointed to by num_addr is " << *num_addr << endl; cout << "Value pointed to by num_addr is " << *num_addr << endl; dist = 158; dist = 158; num_addr = &dist; num_addr = &dist; cout << "Address stored in num_addr is " << num_addr << endl; cout << "Address stored in num_addr is " << num_addr << endl; cout << "Value pointed to by num_addr is " << *num_addr << endl; cout << "Value pointed to by num_addr is " << *num_addr << endl;}

7 Reference variables  A reference variable stores the address of another variable.  Reference variables must be initialized when they are declared. #include #include void main() { int x = 3; int x = 3; int& y = x; int& y = x; cout << "x= " << x << "y = " << y << endl; cout << "x= " << x << "y = " << y << endl; y = 7; y = 7; cout << "x= " << x << "y = " << y << endl; cout << "x= " << x << "y = " << y << endl;}

8 Reference variables (cont.)  Very useful for calling a function by reference. void newval(float& xnum, float& ynum) { xnum = 89.5; xnum = 89.5; ynum = 99.5; ynum = 99.5;}

9 Differences between references and pointers a)A reference parameter is a constant pointer (after initializing a reference parameter, we cannot change it again). b)References are dereferenced automatically (no need to use the dereferencing operator *).

10 int b; // using reference variables int& a = b; a = 10; int b; // using pointers int *a = &b; *a = 10; Differences between references and pointers (cont.)

11 Passing a reference variable to a function b The disadvantage is that the function call does not reveal whether the arguments of the function are reference parameters or not!!

12 #include #include void newval(float&, float&); // function prototype int main() { float firstnum, secnum; float firstnum, secnum; cout << "Enter two numbers: "; cout << "Enter two numbers: "; cin >> firstnum >> secnum; cin >> firstnum >> secnum; newval(firstnum, secnum); newval(firstnum, secnum); cout << firstnum << secnum << endl; cout << firstnum << secnum << endl; return 0; return 0;} void newval(float& xnum, float& ynum) { xnum = 89.5; xnum = 89.5; ynum = 99.5; ynum = 99.5;} Passing a reference variable to a function (cont’d)

13 #include #include void newval(float *, float *); // function prototype int main() { float firstnum, secnum; float firstnum, secnum; cout << "Enter two numbers: "; cout << "Enter two numbers: "; cin >> firstnum >> secnum; cin >> firstnum >> secnum; newval(&firstnum, &secnum); // pass the address explicitly !! newval(&firstnum, &secnum); // pass the address explicitly !! cout << firstnum << secnum << endl; cout << firstnum << secnum << endl; return 0; return 0;} void newval(float *xnum, float *ynum) { *xnum = 89.5; // dereferencing is required !! *xnum = 89.5; // dereferencing is required !! *ynum = 99.5; *ynum = 99.5;} Passing a pointer variable to a function (cont.)

14 Array names as pointers  When an array is created, the compiler automatically creates an internal pointer constant (i.e., we cannot change its contents) for it and stores the starting address of the array in this pointer. b The name of the array becomes the name of the pointer constant.

15 Array names as pointers (cont.) *arr is equivalent to arr[0]

16  Referring to the fourth element of the array cause the compiler, internally, to make the following address computation:  Referring to the fourth element of the array cause the compiler, internally, to make the following address computation: &arr[3] = &arr[0] + (3 * sizeof(int)) (offset to arr[3] = 3 x 2 = 6 bytes)  Alternatively, we can refer to arr[3] as follows:  Alternatively, we can refer to arr[3] as follows: *(arr + 3) Array names as pointers (cont.)

17 #include #include void main() { const SIZE = 5 const SIZE = 5 int i, arr[SIZE] = {98, 87, 92, 79, 85}; int i, arr[SIZE] = {98, 87, 92, 79, 85}; for(i=0; i<SIZE; i++) for(i=0; i<SIZE; i++) cout << arr[i] << *(arr + i) << endl; cout << arr[i] << *(arr + i) << endl;} Array names as pointers (cont.)

18 Static Array Allocation b Static 2D arrays are stored in the memory as 1D arrays..

19 Dynamic Array Allocation  To avoid wasting memory, array allocation or deallocation can take place at run time.  To allocate memory, we need to use the new operator Reserves the number of bytes requested by the declaration. Returns the address of the first reserved location or NULL if sufficient memory is not available.  To deallocate memory (which has previously been allocated using the new operator) we need to use the delete operator. Releases a block of bytes previously reserved. The address of the first reserved location is passed as an argument to the function.

20 case of 1D arrays cout << "Enter array size: "; cin >> SIZE; int *arr; arr = new int[SIZE]; // allocation delete [] arr; // deallocation

21 case of 2D arrays cout << "Enter numRows and numCols: "; cin >> numRows >> numCols; int **arr2D; arr2D = new int* [numRows]; // allocation for(i=0; i<numRows; i++) for(i=0; i<numRows; i++) arr2D[i] = new int[numCols]; arr2D[i] = new int[numCols]; for(i=0; i<numRows; i++) // deallocation delete [] arr2D[i]; delete [] arr2D[i]; delete [] arr2D; (individual elements can be accessed using indices!! (e.g., arr2D[0][2]=10;)) (individual elements can be accessed using indices!! (e.g., arr2D[0][2]=10;))

22 case of 2D arrays (cont.) Understand it !! Very Important diagram; don’t memorize it, Understand it !!

23 Pointer vs. subscript notation Pointer Notation Subscript Notation *arr2D[0] arr2D[0][0] *arr2D[0] arr2D[0][0] *(arr2D[0] + 1) arr2D[0][1] *(arr2D[0] + 1) arr2D[0][1] *(arr2D[0] + 2) arr2D[0][2] *(arr2D[0] + 2) arr2D[0][2] *(arr2D[1]) arr2D[1][0] *(arr2D[1]) arr2D[1][0] *(arr2D[1] + 1) arr2D[1][1] *(arr2D[1] + 1) arr2D[1][1] *(arr2D[1] + 2) arr2D[1][2] *(arr2D[1] + 2) arr2D[1][2]

24 Substituting arr2D[0] with *arr2D and arr2D[1] with *(arr2D+1): Pointer Notation Subscript Notation *(*arr2D) arr2D[0][0] *(*arr2D) arr2D[0][0] *(*arr2D + 1) arr2D[0][1] *(*arr2D + 1) arr2D[0][1] *(*arr2D + 2) arr2D[0][2] *(*arr2D + 2) arr2D[0][2] *(*(arr2D + 1)) arr2D[1][0] *(*(arr2D + 1)) arr2D[1][0] *(*(arr2D + 1) + 1) arr2D[1][1] *(*(arr2D + 1) + 1) arr2D[1][1] *(*(arr2D + 1) + 2) arr2D[1][2] *(*(arr2D + 1) + 2) arr2D[1][2] Pointer vs. subscript notation (cont.)

25 Passing an array to a function b When an array is passed to a function, its address is the only item actually passed to the function. b When an array is passed to a function, its address is the only item actually passed to the function. Passing a 1D array to a function #include #include float find_average(int *, int); void main() { int *arr; int *arr; arr = new int[numElems]; arr = new int[numElems]; arr[0] = 2; arr[1] = 18; arr[2] = 1; arr[0] = 2; arr[1] = 18; arr[2] = 1; arr[3] = 27; arr[4]= 16; arr[3] = 27; arr[4]= 16; // or using static memory allocation: const numElems = 5; int arr[numElems] = {2, 18, 1, 27, 16}; cout << "The average is " << find_average(arr, numElems) << endl; cout << "The average is " << find_average(arr, numElems) << endl;} (continues)

26 float find_average(int *vals, int n) { int i; int i; float avg; float avg; avg=0.0; avg=0.0; for(i=0; i<n; i++) for(i=0; i<n; i++) avg += vals[i]; avg += vals[i]; avg = avg/n; avg = avg/n; return avg; return avg;} Passing a 1D array to a function (cont.)

27 #include #include float find_average(int **, int, int); void main() { int **arr2D; int **arr2D; float average; float average; arr2D = new int*[numRows]; arr2D = new int*[numRows]; for(i=0; i<numRows; i++) for(i=0; i<numRows; i++) arr2D[i] = new int[numCols]; arr2D[i] = new int[numCols]; // using static array allocation will not work in this case !! //const numRows = 2; // const numCols = 6; // int arr2D[numRows][numCols] = { {25, 29, 15, 95, 22, 21}, // {105, 254, 125, 25, 925, 253} }; // {105, 254, 125, 25, 925, 253} }; (continues) Passing a 2D array to a function

28 average = find_average(arr2D, numRows, numCols); cout << "The average is " << average << endl; cout << "The average is " << average << endl;} float find_average(int **vals, int n, int m) { int i, j; int i, j; float avg; float avg; avg=0.0; avg=0.0; for(i=0; i<n; i++) for(i=0; i<n; i++) for(j=0; j<m; j++) for(j=0; j<m; j++) avg += vals[i][j]; avg += vals[i][j]; avg = avg/(n*m); avg = avg/(n*m); return avg; return avg;} Passing a 2D array to a function (cont.)


Download ppt "Pointers CSE 5100 Data Structures and Algorithms."

Similar presentations


Ads by Google