Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 18: The Elegant, Abstract World of Computing

Similar presentations


Presentation on theme: "Lecture 18: The Elegant, Abstract World of Computing"— 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. 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;

5 Passing Arrays by Reference
Array name like a constant pointer 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 In main(), just before call into sumRef()

6 Passing Arrays by Reference
Array name like a constant pointer 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 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C In main(), while calling into sumRef()

7 Passing Arrays by Reference
Array name like a constant pointer 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 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C In sumRef(), just after execute “*sum = 0;”

8 Passing Arrays by Reference
Array name like a constant pointer 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 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C In sumRef(), …

9 Passing Arrays by Reference
Array name like a constant pointer 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 1 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C In sumRef(), after execute “*sum += x[k];”

10 Passing Arrays by Reference
Array name like a constant pointer 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 1 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 1 In sumRef(), increment k

11 Passing Arrays by Reference
Array name like a constant pointer 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 3 1 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 1 1 In sumRef(), perform addition

12 Passing Arrays by Reference
Array name like a constant pointer 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 3 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 2 1 In sumRef(), increment k

13 Passing Arrays by Reference
Array name like a constant pointer 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 6 3 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 2 In sumRef(), perform addition

14 Passing Arrays by Reference
Array name like a constant pointer 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 15 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 0xBFFFF840 k 0xBFFFF85C 5 In sumRef(), right after the for loop

15 Passing Arrays by Reference
Array name like a constant pointer 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 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 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 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 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 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 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 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 k 0xBFFFF85C 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 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 1 k 0xBFFFF85C 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 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 1 k 0xBFFFF85C 1 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 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 1 3 k 0xBFFFF85C 1 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 x 0xBFFFF850 0xBFFFF82C size 0xBFFFF854 5 sum 0xBFFFF858 15 k 0xBFFFF85C 5 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 In main(), right after call sumRef()

25 In-class Programming Assignment
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]; 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); x = bPtr[2]; x = *(b+2); b = b + 2; x = *b; bPtr += 2; x = *bPtr; Solution: D


Download ppt "Lecture 18: The Elegant, Abstract World of Computing"

Similar presentations


Ads by Google