180 likes | 340 Views
Lecture 17: The Last Puzzle Piece with Functions. Pointers . Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings. Pointer Variables. Contain memory addresses as their values
E N D
Pointers • Powerful, but difficult to master • Simulate call-by-reference • Close relationship with arrays and strings
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. RAM … count 0xBFFFF818 15 … countPtr 0xBFFF924 BFFFF818 …
is a pointer variable and is of type int * is NOT a pointer and is of type int Pointer Variables Definitions RAM … • * used to declarepointer 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 … countPtr 0xBFFF924 BFFFF818 …
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); RAM … y 0xBFFFF818 5 yPtr2 0xBFFF924 BFFFF818 … yPtr 0xBFFF924 BFFFF818 …
Pointer Operators RAM • 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 7 15 x 0xBFFFF81C 5 … yPtr 0xBFFF924 BFFFF818 …
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.
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<stdio.h> 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; } RAM … a 0xBFFFF818 5 result 0xBFFFF81C 0 … … Just before call into cubeValue()
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<stdio.h> 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; } RAM … a 0xBFFFF818 5 result 0xBFFFF81C 0 … x 0xBFFFF828 5 … In cubeValue(), just before perform the cube operation
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<stdio.h> 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; } RAM … a 0xBFFFF818 5 result 0xBFFFF81C 0 … x 0xBFFFF828 125 … In cubeValue(), just before return to main()
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<stdio.h> 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; } RAM … a 0xBFFFF818 5 result 0xBFFFF81C 125 … … In main(), just after call into cubeValue()
Call-by-Reference Passes original argument Changes in function effect original Using pointers and the dereferencing operator to simulate call-by-reference in C. RAM #include<stdio.h> 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 … … In main(), just before call into cubeRef()
Call-by-Reference Passes original argument Changes in function effect original Using pointers and the dereferencing operator to simulate call-by-reference in C. RAM #include<stdio.h> 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 … x 0xBFFFF828 0xBFFFF818 … In cubeRef(), just before perform the cube operation
Call-by-Reference Passes original argument Changes in function effect original Using pointers and the dereferencing operator to simulate call-by-reference in C. RAM #include<stdio.h> 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 125 … x 0xBFFFF828 0xBFFFF818 … In cubeRef(), just before return to main()
Call-by-Reference Passes original argument Changes in function effect original Using pointers and the dereferencing operator to simulate call-by-reference in C. RAM #include<stdio.h> 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 125 … … In main(), just after call into cubeRef()
In-Class Programming Assignment #include <stdio.h> #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]) { }
Practice Question Q. What is the output of the following program? Solution: B
Practice Question Q. What is the output of the following program? Solution: C