Download presentation
Presentation is loading. Please wait.
Published byGary Miles Modified over 8 years ago
1
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4; b=7; add_one(a,b); printf("a=%i b=%i",a,b); return 0; } What is printed? a=4 b=7
2
What is going on ? 142 L -2 ab main xy add_one 4 4578 7 XX (doesn't return anything!)
3
142 L -3 Values versus locations 4 a 1012 Consider a memory box: If we want add_one to really add 1 to a and b (and not just to a copy of their value), add_one needs access to the locations of a and b as well as to their value. to know where the memory boxes are. to know what the boxes contain Variable name for a location which holds a value
4
142 L -4 Can we have access to the location number (= address) of a variable? Yes! Use a pointer New type: pointer variable a pointer contains the reference to another variable, i.e. the value of a pointer is the address of a variable Example: int x; regular integer variable x 24 10115 int *xp; xp is a variable of type pointer to an integer xp 97968 to assign the address of x to xp: xp=&x; 10115
5
142 L - 5 Using a pointer relevant unary operator: & and * read & as: address of read * as: content of the location pointed to by xp = &x; assign the address of x to xp *xp = 0; *xp = *xp + 1; assign integer 0 to x add 1 to x
6
142 L -6 add_one with pointers void add_one(int *xp, int *yp) { *xp = *xp + 1; *yp = *yp + 1; } int main(void) { int a,b; a=4; b=7; add_one(&a,&b); printf("a=%i b=%i",a,b); return 0; } What is printed? a=5 b=8
7
4 What is going on ? 142 L -7 7 a b main 101 102 xp yp add_one 101 102 points to a points to b 58 545 546
8
142 L -8 Addresses and Pointers Three new types: int * pointer to an int double * pointer to a double char * pointer to a char Two new unary operators: & address of (can be applied to any variable) * content of the location pointed to by (can be applied ONLY to a pointer) Note: to print the value of a pointer, use the placeholder %p printf("&i=%p",&i);
9
142 L -9 Vocabulary Dereferencing: Following a pointer to a memory location Output parameters: int *iptr; int i,j; i=0; iptr = &i; j = *iptr + 1; e.g. dereferencing use iptr to point to i same as j=i+1; up to now, parameters of a function have always been input parameters. With a pointer as a parameter of a function: _ as usual can provide a value to the function (input) _ can get a value back from the function via a parameter (output). Before, could get only one value back from a function. With pointers, as many as we want. void add_one(int *xp, int *yp){...} add_one(&a,&b); input and output
10
Why use pointers? 142 L -10 as output parameters Functions that need to change the value of their actual parameters (e.g. add_one ) to get multiple return values Functions that need to pass back several results Advanced programming Operations on arrays, linked lists (see chapter 14)
11
An Example 142 L -11 Swap 2 integers: Given c1 and c2, exchange their values if c2<c1 Without pointers: int c1, c2, temp; printf("Enter two integers: "); scanf("%i %i",&c1,&c2); if (c2<c1) { temp = c1; c1 = c2; c2 = temp; } With pointers: we can use a function if (c2<c1) swap(&c1,&c2); replace that with this and for the swap function void swap(int *p1, int *p2) { int temp; temp = *p1; *p1 = *p2; *p2 = temp; }
12
Another Example (1) 142 L -12 Order 3 integers: Given x,y and z, order x, y and z in increasing order One way (using swap from the previous example): int x,y,z; printf("Enter three integers: "); scanf("%i %i %i",&x, &y, &z); if (x>y) swap(&x,&y); if (x>z) swap(&x,&z); if (y>z) swap(&y,&z); order counts!
13
Another way: Having just one function call in main int main(void) { int x,y,z; printf("Enter three integers: "); scanf("%i %i %i",&x, &y, &z); sort3(&x,&y,&z);... } void sort3(int *xp, int *yp, int *zp) { if (*xp>*yp) swap(xp,yp); if (*xp>*zp) swap(xp,zp); if (*yp>*zp) swap(yp,zp); } no & 142 L -13 Another Example (2)
14
Beware of types ! 142 L -14 int i; int *ip; double x; double *xp; x = i;OK i = x;Not recommended ip = 30; No! ip = i; No! ip = &i;OK ip = &x;No! xp = ip;No! &i = ip;Meaningless (compilation error)
15
Understand & in scanf 142 L - 15 A function to read the letter a or b: void scan_a_or_b(char *chp) { printf("Enter an 'a' or a 'b': "); scanf("%c",chp); while(*chp != 'a' && *chp != 'b') { printf("What?\n"); printf ("Enter an 'a' or a 'b': "); scanf("%c",chp); } and in main: char answer; scan_a_or_b(&answer); No & don't say Always use & in scanf say Always use an address in scanf
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.