1 / 35

Principles of Computer Science I Honors Section

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.

wayde
Download Presentation

Principles of Computer Science I Honors Section

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Principles of Computer Science IHonors Section Note Set 7 CSE 1341

  2. Today: Intro to Pointers

  3. 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

  4. 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

  5. Pointers

  6. 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;

  7. 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;

  8. Pointers int x = 25; int* ptrX; ptrX = &x; Holds an address to an int 25 1b24 Holds an int 1b24 1c54 x ptrX

  9. Pointers 25 1b24 1b24 1c54 x ptrX cout << x << endl; cout << &x << endl; cout << ptrX << endl; cout << *ptrX << endl; 25 1b24 1b24 25

  10. 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

  11. 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

  12. Relationship Between Pointers and Arrays

  13. 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]

  14. 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))

  15. 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?

  16. 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]

  17. Pointer Offset Notation int abc[] = {3, 5, 7, 9, 11}; *(abc + 3) abc[3]

  18. 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;

  19. 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[]

  20. 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);

  21. Overview 2D Arrays and Pointers

  22. 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

  23. 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]

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. Using 2-D Arrays

  30. 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. . .

  31. 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. . .

  32. 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. . .

  33. 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; }

  34. 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; }

  35. Questions??? ?

More Related