270 likes | 410 Views
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.
E N D
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 • 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]
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;
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()
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()
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;”
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(), …
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];”
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
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
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
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
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
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()
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()
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()
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;”
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(), …
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];”
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
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
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
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()
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]){ }
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
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