1 / 27

Lecture 18: The Elegant, Abstract World of Computing

Lecture 18: The Elegant, Abstract World of Computing. Arrays and Pointers . Arrays and pointers closely related Array name like a constant pointer Arrays passed to a function by reference Pointers can do array subscripting operations. Relationship between Pointers and Arrays.

bessie
Download Presentation

Lecture 18: The Elegant, Abstract World of Computing

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. Lecture 18: The Elegant, Abstract World of Computing

  2. Arrays and Pointers • Arrays and pointers closely related • Array name like a constant pointer • Arrays passed to a function by reference • Pointers can do array subscripting operations

  3. Relationship between Pointers and Arrays • Define an array b[5] and a pointer bPtr int b[5]; int *bPtr; • To set them equal to one another use bPtr = b; • The array name (b) is actually the address of first element of the array. bPtr = &b[ 0 ]; • Explicitly assigns the address of first element of b to bPtr. • Element b[ 3 ] • Can be accessed by *(bPtr + 3)--- pointer/offset notation where 3 is called the offset. • Can be accessed by bPtr[ 3 ] --- pointer/subscript notation bPtr[ 3 ] same as b[ 3 ] • Can also be accessed by *(b+3) --- view b as a constant pointer *b accesses to b[0]

  4. Relationship between Pointers and Arrays • What is the difference? int b[5], a[5]; int *objPtr; • A pointer can be assigned to point to different objects. objPtr = b; objPtr = a; • Array name likes a constant pointer. objPtr = b; a = objPtr; b = a; • Pointers • Dereferencing a NULL pointer is NOT allowed. int *aPtr = NULL, *bPtr = 0; int x; x = *aPtr; x = * bPtr; • Dereferencing a pointer not pointing to an object is NOT allowed. int *aPtr , x; x = *aPtr;

  5. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … … In main(), just before call into sumRef()

  6. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C … In main(), while calling into sumRef()

  7. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 0 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C … In sumRef(), just after execute “*sum = 0;”

  8. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 0 … In sumRef(), …

  9. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 1 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 0 … In sumRef(), after execute “*sum += x[k];”

  10. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 1 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 1 0 … In sumRef(), increment k

  11. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 1 3 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 1 1 … In sumRef(), perform addition

  12. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 3 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 1 2 … In sumRef(), increment k

  13. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 3 6 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 2 0 … In sumRef(), perform addition

  14. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 15 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 5 0 … In sumRef(), right after the for loop

  15. Passing Arrays by Reference RAM Array name like a constant pointer … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int *sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, &total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int *sum ) { int k; *sum = 0; for (k = 0; k < size; k++) { *sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 15 … … In main(), right after call sumRef()

  16. Call-by-Value via Call-by-Reference Passing array by reference Passing integer/char/float by value RAM … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int sum ) { int k; sum = 0; for (k = 0; k < size; k++) { sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … … In main(), just before call into sumRef()

  17. Call-by-Value via Call-by-Reference Passing array by reference Passing integer/char/float by value RAM … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int sum ) { int k; sum = 0; for (k = 0; k < size; k++) { sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0 k 0xBFFFF85C … In main(), while calling into sumRef()

  18. Call-by-Value via Call-by-Reference Passing array by reference Passing integer/char/float by value RAM … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int sum ) { int k; sum = 0; for (k = 0; k < size; k++) { sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0 0 k 0xBFFFF85C … In sumRef(), just after execute “sum = 0;”

  19. Call-by-Value via Call-by-Reference Passing array by reference Passing integer/char/float by value RAM … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int sum ) { int k; sum = 0; for (k = 0; k < size; k++) { sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0 k 0xBFFFF85C 0 … In sumRef(), …

  20. Call-by-Value via Call-by-Reference Passing array by reference Passing integer/char/float by value RAM … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int sum ) { int k; sum = 0; for (k = 0; k < size; k++) { sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0 1 k 0xBFFFF85C 0 … In sumRef(), after execute “sum += x[k];”

  21. Call-by-Value via Call-by-Reference Passing array by reference Passing integer/char/float by value RAM … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int sum ) { int k; sum = 0; for (k = 0; k < size; k++) { sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 1 k 0xBFFFF85C 1 0 … In sumRef(), increment k

  22. Call-by-Value via Call-by-Reference Passing array by reference Passing integer/char/float by value RAM … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int sum ) { int k; sum = 0; for (k = 0; k < size; k++) { sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 1 3 k 0xBFFFF85C 1 0 … In sumRef(), perform addition

  23. Call-by-Value via Call-by-Reference Passing array by reference Passing integer/char/float by value RAM … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int sum ) { int k; sum = 0; for (k = 0; k < size; k++) { sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 15 k 0xBFFFF85C 5 0 … In sumRef(), right after the for loop

  24. Call-by-Value via Call-by-Reference Passing array by reference Passing integer/char/float by value RAM … #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d\n”, total); return 0; } void sumRef( int *x, int size, int sum ) { int k; sum = 0; for (k = 0; k < size; k++) { sum += x[k]; } return; } a[0] 0xBFFFF82C 1 a[1] 0xBFFFF830 2 a[2] 0xBFFFF834 3 a[3] 0xBFFFF838 4 a[4] 0xBFFFF83C 5 total 0xBFFFF840 0 … … In main(), right after call sumRef()

  25. In-class Programming Assignment 7.034 0.361 2.550 11.588 19.166 18.317 9.821 1.490 0.983 8.766 17.684 19.537 12.622 3.296 0.134 6.043 15.590 19.998 15.214 5.636 0.071 3.634 13.050 j #include <stdio.h> #define SIZE 3000 #define WINDOW 10 void readin(double sonar[SIZE]); void denoising(double *sonarPtr, double *rPtr); int main (){ double sonar[SIZE]; double denoisedSonar[SIZE]; readin(sonar); denoising(sonar, denoisedSonar); return 0; } void denoising(double *sonarPtr, double *rPtr) { /* You need to finish this function */ For every possible starting index j of the sliding window, sum = 0; compute the total sum of the sonar data inside the sliding window; rPtr[j] = sum / WINDOW; } void readin(double sonar[SIZE]){ }

  26. Practice Question Q. What is the output of the following program? #include<stdio.h> #define SIZE 5 void sumRef( int *x, int size, int sum ); int main( void ) { int a[SIZE] = {1, 2, 3, 4, 5}; int total = 0; sumRef( a, SIZE, total ); printf(“%d %d\n”, *a, total); return 0; } void sumRef( int *x, int size, int sum ) { int k; sum = 0; for (k = 0; k < size; k++) { sum += x[k]; } for (k = 0; k < size; k++) { x[k] *= 3; } } 3 15 1 15 3 0 1 0 Solution: C

  27. Practice Question Q. To assign the third element b[2] of an array b to the variable x, which of the following is WRONG? int x, b[5] = {1, 2, 3, 4, 5}; int *bPtr; bPtr = b; x = *(bPtr+2); bPtr = b; x = bPtr[2]; x = *(b+2); b = b + 2; x = *b; bPtr = b; bPtr += 2; x = *bPtr; Solution: D

More Related