350 likes | 424 Views
Principles of Computer Science I Honors Section. Note Set 7 CSE 1341. Today:. Intro to Pointers. Address Operator (&). & operator gives the memory address of a variable. x. 1b00. 25. 1b04. 1b08. int x = 25;. 1b0c. 1b10. What is the address of x ??. 1b14. 1b18. 1b00. 1b1c.
E N D
Principles of Computer Science IHonors Section Note Set 7 CSE 1341
Today: Intro to Pointers
Address Operator (&) • & operator gives the memory address of a variable x 1b00 25 1b04 1b08 int x = 25; 1b0c 1b10 What is the address of x?? 1b14 1b18 1b00 1b1c
Address Operator (&) • Gives the memory address of a variable x 1b00 25 1b04 int x = 25; 1b08 cout << x << endl; cout << &x << endl; 1b0c 1b10 25 1b00 1b14 1b18 1b1c
Pointers • A pointer variable (pointer) is a variable that holds and allows manipulation of memory addresses. Holds an address where an int is stored int * ptrX;
Holds an address to an int 25 Holds an int 1b24 1c54 x ptrX Pointers sets up storage for one integer and one pointer to an integer int x = 25; int* ptrX;
Pointers int x = 25; int* ptrX; ptrX = &x; Holds an address to an int 25 1b24 Holds an int 1b24 1c54 x ptrX
Pointers 25 1b24 1b24 1c54 x ptrX cout << x << endl; cout << &x << endl; cout << ptrX << endl; cout << *ptrX << endl; 25 1b24 1b24 25
Dereference Operator If you have an address, you can use the ‡dereference operator (*) to access what is stored at that address. int x = 12; int *myPtr = &x; cout << *myPtr; *myPtr = 12345; ‡ Book calls this the indirection operator
Pointers 25 1b24 1b24 1c54 x ptrX Use the indirection operator to access value pointed to cout << *ptrX << endl; *ptrX = 100; cout << *ptrX << endl; 25 100
Pointers and Arrays short n[5] = {10, 20, 30, 40, 50}; 10 3000 3000 n[0] n 20 3002 n[1] 30 3004 n[2] The name of the array holds the address of the 1st element in the array 40 3006 n[3] 50 3008 n[4]
Pointers and Arrays 3000 n In c++, when you add a value to a pointer, you are adding that value times the size of the data type being referenced by that pointer. n n+1 n+3 3000 3002 3006 n + 1 = 3000 + (1 * sizeof(short)) n + 3 = 3000 + (3 * sizeof(short))
Pointers and Arrays short n[5] = {10, 20, 30, 40, 50}; 10 3000 3000 n[0] n 20 3002 n[1] 30 *n *(n+1) *(n+3) 10 20 40 3004 n[2] 40 3006 n[3] 50 3008 n[4] Must use parentheses here. Why?
Pointers and Arrays short n[5] = {10, 20, 30, 40, 50}; 10 pointer offset 3000 n[0] subscript 20 *n *(n+1) *(n+3) n[0] n[1] n[3] 3002 n[1] 30 3004 n[2] 40 3006 n[3] 50 3008 n[4]
Pointer Offset Notation int abc[] = {3, 5, 7, 9, 11}; *(abc + 3) abc[3]
Pointers and Arrays int numbers[5]; for(int i = 0; i < 5; i++) cin >> *(numbers + i); for(int i = 0; i < 5; i++) cout << *(numbers + i) << endl; cin >> numbers[i]; cout << numbers[i] << endl;
Passing Array by Pointer int main() { float sales[4]; getSales(sales); return 0; } void getSales(float* array) { for (int i = 0; i < 4; i++) cin >> array[i]; } float array[]
Exercise Write a function that will print the elements of the array passed as a parameter backwards. Use pointer offset notation. void backwards(int *arr, int size);
Overview 2D Arrays and Pointers
2-D Arrays short zippo[4][2] = {5, 9, 8, 12, 11, 3, 1, 0}; 5 9 zippo[1][1] _______ zippo[3][0] _______ zippo[2][1] _______ zippo[3][2] _______ 8 12 11 3 1 0
2-D Arrays short zippo[4][2] = {5, 9, 8, 12, 11, 3, 1, 0}; 5 9 zippo[0] 8 12 zippo[1] 11 3 zippo[2] 1 0 zippo[3]
2-D Arrays 5 3214 5 9 9 3216 3214 8 3218 8 12 3218 12 3220 is stored as 11 3 11 3222 3222 3 3224 1 0 3226 1 3226 0 3228
2-D Arrays 5 9 3214 zippo[0] -> 3214 zippo[1] -> 3218 zippo[2] -> 3222 zippo[3] -> 3226 8 12 3218 11 3 3222 1 0 3226
2-D Array and Pointers 5 9 Task: Display the 8 Old Way: cout << zippo[1][0]; New Way: cout << *zippo[1]; 3214 8 12 3218 11 3 3222 1 0 3226
2-D Array and Pointers 5 9 Task: Display the 3 Old Way: cout << zippo[2][1]; New Way: cout << *(zippo[2]+1); 3214 8 12 3218 11 3 3222 1 0 3226
2-D Array and Pointers 5 9 3214 zippo[1] + 4 _____ *(zippo[1] + 4) _____ *(zippo[2] + 2) _____ 8 12 3218 11 3 3222 1 0 3226
Using a 2-D Arrays int oz[3][4]={ {2,4,58}, {3,5,69}, {12,10,8,6} }; for (int i = 0; i < 3; i++) { for(int j = 0; j < 4; j++) cout << oz[i][j] << “ “; cout << endl; } the old way. . .
Using a 2-D Arrays int oz[3][4]={{2,4,58}, {3,5,69}, {12,10,8,6} }; for (int i = 0; i < 3; i++) { for(int j = 0; j < 4; j++) cout << *(oz[i]+j) << “ “; cout << endl; } a new way. . .
Using a 2-D Arrays int oz[3][4]={ {2,4,58},{3,5,69}, {12,10,8,6} }; for (int i = 0; i < 3; i++) { for(int j = 0; j < 4; j++) cout << *(*(oz+i) + j) << “ “; cout << endl; } a new way. . .
Using a 2-D Arrays int main() { int oz[3][4]={ {2,4,58},{3,5,69}, {12,10,8,6} }; multiply(oz,3); return 0; } Write a function that multiplies every element of arr by 2. . . void multiply(int arr[][4], int rows) { for(int i = 0; i < rows; i++) for(int j = 0; j < 4; j++) *( *(arr + i) + j) *= 2; }
Using a 2-D Arrays Write a function that performs the same operation, but uses a different type of pointer notation . . . void multiply(int arr[][4], int rows) { for(int i = 0; i < rows; i++) for(int j = 0; j < 4; j++) *(arr[i] + j) *= 2; }