Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 17: The Last Puzzle Piece with Functions.

Similar presentations


Presentation on theme: "Lecture 17: The Last Puzzle Piece with Functions."— Presentation transcript:

1 Lecture 17: The Last Puzzle Piece with Functions

2 Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings

3 Pointer Variables Contain memory addresses as their values Normal variables contain a specific value (directly reference a value) Pointers contain address of a variable that has a specific value (indirectly reference a value) Referencing a value through a pointer is called indirection countPtr is said to “point to” count. … … … count 0xBFFFF818 15 BFFFF818 RAM countPtr 0xBFFF924

4 Pointer Variables Definitions * used to declare pointer variables int *countPtr; Defines a pointer to an int (countPtr is of type int *) Can define pointers to any data type float *floatPtr; char *charPtr; Multiple pointers require using a * before each variable definition. int *countPtr, *indexPtr; int *subPtr, count ; … … … count 0xBFFFF818 15 BFFFF818 RAM countPtr 0xBFFF924 is a pointer variable and is of type int * is NOT a pointer and is of type int

5 Pointer Variables Initialization Initialize pointers to 0, NULL, or an address Any pointer can be initialized as 0 or NULL Points to nothing (NULL preferred) countPtr = 0; countPtr = NULL; floatPtr = NULL; floatPtr = 50000; Initialize to the address of a variable. int y = 5, x; int *yPtr = &y; int *yPtr2 = &y; float *fPtr = &y; int *cPtr = &(100); int *aPtr = &(x + y); … … … y 0xBFFFF818 5 BFFFF818 RAM yPtr 0xBFFF924 BFFFF818 yPtr2 0xBFFF924

6 Pointer Operators Indirection/dereferencing operator ( * ) int y = 5, x; int *yPtr = &y; Returns the value of the object to which its operand (i.e., a pointer) points printf(“%d”, *yPtr); x = *yPtr; Using * in this manner is called dereferencing a pointer. * can be used for assignment *yPtr = 7; /* changes y to 7 */ printf(“%d”, y); y = 15; printf(“%d”, *yPtr); * and & are inverses; They cancel each other out. printf(“%p”, yPtr); printf(“%p”, &*yPtr); printf(“%p”, *&yPtr); … … … y 0xBFFFF818 5 BFFFF818 RAM yPtr 0xBFFF924 x 0xBFFFF81C 5 7 15

7 Calling Functions Two ways to pass arguments to a function Call-by-value Call-by-reference All arguments in C are passed by value The return statement used to return one value from a called function to a caller. Call-by-reference is needed Modification of one or more variables in the caller is needed. Avoid the overhead of passing a large data object by value. Using pointers and the dereferencing operator to simulate call-by-reference in C.

8 Call-by-Value  A Review Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument #include int cubeValue( int x ); int main( void ) { int a = 5; int result = 0; result = cubeValue( a ); printf(“%d\n”, result); return 0; } int cubeValue( int x ) { x = x * x * x; return x; } … … … a 0xBFFFF818 5 RAM result 0xBFFFF81C 0 Just before call into cubeValue()

9 Call-by-Value  A Review Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument #include int cubeValue( int x ); int main( void ) { int a = 5; int result = 0; result = cubeValue( a ); printf(“%d\n”, result); return 0; } int cubeValue( int x ) { x = x * x * x; return x; } … … … a 0xBFFFF818 5 RAM result 0xBFFFF81C 0 In cubeValue(), just before perform the cube operation x 0xBFFFF828 5

10 Call-by-Value  A Review Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument #include int cubeValue( int x ); int main( void ) { int a = 5; int result = 0; result = cubeValue( a ); printf(“%d\n”, result); return 0; } int cubeValue( int x ) { x = x * x * x; return x; } … … … a 0xBFFFF818 5 RAM result 0xBFFFF81C 0 In cubeValue(), just before return to main() x 0xBFFFF828 125

11 Call-by-Value  A Review Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument #include int cubeValue( int x ); int main( void ) { int a = 5; int result = 0; result = cubeValue( a ); printf(“%d\n”, result); return 0; } int cubeValue( int x ) { x = x * x * x; return x; } … … … a 0xBFFFF818 5 RAM result 0xBFFFF81C 125 In main(), just after call into cubeValue()

12 Call-by-Reference Passes original argument Changes in function effect original Using pointers and the dereferencing operator to simulate call-by-reference in C. #include void cubeRef( int *x ); int main( void ) { int a = 5; cubeRef( &a ); printf(“%d\n”, a); return 0; } void cubeRef( int *x ) { *x = (*x) * (*x) * (*x); } … … … a 0xBFFFF818 5 RAM In main(), just before call into cubeRef()

13 Call-by-Reference Passes original argument Changes in function effect original Using pointers and the dereferencing operator to simulate call-by-reference in C. … … … a 0xBFFFF818 5 RAM In cubeRef(), just before perform the cube operation x 0xBFFFF828 0xBFFFF818 #include void cubeRef( int *x ); int main( void ) { int a = 5; cubeRef( &a ); printf(“%d\n”, a); return 0; } void cubeRef( int *x ) { *x = (*x) * (*x) * (*x); }

14 Call-by-Reference Passes original argument Changes in function effect original Using pointers and the dereferencing operator to simulate call-by-reference in C. … … … a 0xBFFFF818 125 RAM In cubeRef(), just before return to main() x 0xBFFFF828 #include void cubeRef( int *x ); int main( void ) { int a = 5; cubeRef( &a ); printf(“%d\n”, a); return 0; } void cubeRef( int *x ) { *x = (*x) * (*x) * (*x); } 0xBFFFF818

15 Call-by-Reference Passes original argument Changes in function effect original Using pointers and the dereferencing operator to simulate call-by-reference in C. … … … a 0xBFFFF818 125 RAM In main(), just after call into cubeRef() #include void cubeRef( int *x ); int main( void ) { int a = 5; cubeRef( &a ); printf(“%d\n”, a); return 0; } void cubeRef( int *x ) { *x = (*x) * (*x) * (*x); }

16 In-Class Programming Assignment #include #define SIZE 100 void readin(double sonar[3][SIZE]); void speedCtl(double left, double front, double right, int *driveLptr, int *driveRptr); //Function for data analysis here int main (){ double sonar[3][SIZE]; int driveL, driveR; int k = 0; readin(sonar); while (k < 50) { speedCtl(sonar[0][k], sonar[1][k], sonar[2][k], &driveL, &driveR); printf(“(%d, %d)\n”, driveL, driveR); } printf(“(0, 0)\n”); return 0; } void speedCtl(double left, double front, double right, int *driveLptr, int *driveRptr) {/*You need to finish this function to control the speed of the motors here.*/ } void readin(double sonar[][SIZE]) { }

17 Practice Question Q. What is the output of the following program? Solution: B

18 Practice Question Q. What is the output of the following program? Solution: C


Download ppt "Lecture 17: The Last Puzzle Piece with Functions."

Similar presentations


Ads by Google