Lecture 17: The Last Puzzle Piece with Functions.

18
Lecture 17: The Last Puzzle Piece with Functions

Transcript of Lecture 17: The Last Puzzle Piece with Functions.

Page 1: Lecture 17: The Last Puzzle Piece with Functions.

Lecture 17: The Last Puzzle Piece with Functions

Page 2: Lecture 17: The Last Puzzle Piece with Functions.

PointersPowerful, but difficult to masterSimulate call-by-referenceClose relationship with arrays and strings

Page 3: Lecture 17: The Last Puzzle Piece with Functions.

Pointer Variables

Contain memory addresses as their valuesNormal 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.

……

count0xBFFFF818

15

BFFFF818

RAM

countPtr0xBFFF924

Page 4: Lecture 17: The Last Puzzle Piece with Functions.

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 ;

……

count0xBFFFF818

15

BFFFF818

RAM

countPtr0xBFFF924

is a pointer variable and is of type int *

is NOT a pointer and is of type int

Page 5: Lecture 17: The Last Puzzle Piece with Functions.

Pointer Variables InitializationInitialize pointers to 0, NULL, or an address

Any pointer can be initialized as 0 or NULLPoints 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);

……

y0xBFFFF818

5

BFFFF818

RAM

yPtr0xBFFF924

BFFFF818yPtr20xBFFF924

Page 6: Lecture 17: The Last Puzzle Piece with Functions.

Pointer OperatorsIndirection/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);

……

y0xBFFFF818

5

BFFFF818

RAM

yPtr0xBFFF924

x0xBFFFF81C

5

715

Page 7: Lecture 17: The Last Puzzle Piece with Functions.

Calling Functions

Two ways to pass arguments to a functionCall-by-valueCall-by-reference

All arguments in C are passed by valueThe return statement used to return one value from a called function to a caller.

Call-by-reference is neededModification 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.

Page 8: Lecture 17: The Last Puzzle Piece with Functions.

Call-by-Value A Review

Copy of argument passed to functionChanges in function do not effect originalUse 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; }

……

a0xBFFFF818

5

RAM

result0xBFFFF81C

0

Just before call into cubeValue()

Page 9: Lecture 17: The Last Puzzle Piece with Functions.

Call-by-Value A Review

Copy of argument passed to functionChanges in function do not effect originalUse 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; }

……

a0xBFFFF818

5

RAM

result0xBFFFF81C

0

In cubeValue(), just before perform the cube operation

x0xBFFFF828

5

Page 10: Lecture 17: The Last Puzzle Piece with Functions.

Call-by-Value A Review

Copy of argument passed to functionChanges in function do not effect originalUse 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; }

……

a0xBFFFF818

5

RAM

result0xBFFFF81C

0

In cubeValue(), just before return to main()

x0xBFFFF828

125

Page 11: Lecture 17: The Last Puzzle Piece with Functions.

Call-by-Value A Review

Copy of argument passed to functionChanges in function do not effect originalUse 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; }

……

a0xBFFFF818

5

RAM

result0xBFFFF81C

125

In main(), just after call into cubeValue()

Page 12: Lecture 17: The Last Puzzle Piece with Functions.

Call-by-Reference

Passes original argumentChanges in function effect originalUsing pointers and the dereferencing operator to simulate call-by-reference in C.

#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);}

……

a0xBFFFF818

5

RAM

In main(), just before call into cubeRef()

Page 13: Lecture 17: The Last Puzzle Piece with Functions.

Call-by-Reference

Passes original argumentChanges in function effect originalUsing pointers and the dereferencing operator to simulate call-by-reference in C.

……

a0xBFFFF818

5

RAM

In cubeRef(), just before perform the cube operation

x0xBFFFF828

0xBFFFF818

#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);}

Page 14: Lecture 17: The Last Puzzle Piece with Functions.

Call-by-Reference

Passes original argumentChanges in function effect originalUsing pointers and the dereferencing operator to simulate call-by-reference in C.

……

a0xBFFFF818

125

RAM

In cubeRef(), just before return to main()

x0xBFFFF828

#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);}

0xBFFFF818

Page 15: Lecture 17: The Last Puzzle Piece with Functions.

Call-by-Reference

Passes original argumentChanges in function effect originalUsing pointers and the dereferencing operator to simulate call-by-reference in C.

……

a0xBFFFF818

125

RAM

In main(), just after call into cubeRef()

#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);}

Page 16: Lecture 17: The Last Puzzle Piece with Functions.

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]){}

Page 17: Lecture 17: The Last Puzzle Piece with Functions.

Practice Question

Q. What is the output of the following program?

#include <stdio.h> int nosense(int *x, int y); int main() { int a = 2; int b = 3; nosense(&a, b); printf ("a + b = %d\n", a + b); return 0; } int nosense(int *x, int y) { *x = *x * y; y += *x; return (x+y); }

A) a + b = 5 B) a + b = 9 C) a + b = 15 D) a + b = 11

Solu

tion

: B

Page 18: Lecture 17: The Last Puzzle Piece with Functions.

Practice Question

Q. What is the output of the following program?

#include <stdio.h> int main() { int a = 2; int *aPtr = &a; printf ("%d ", *aPtr); a *= 3; printf(“%d ”, *aPtr); *aPtr += 4; printf(“%d”, a); return 0; }

A) 2 2 2 B) 2 6 6 C) 2 6 10 D) 0 6 2

Solu

tion

: C