CS110_06c_pointers3 - EECS110: 6c Pointers: &, *,...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS110: 6c Pointers: &, *, and Functions Jack Tumblin jet@cs.northwestern.edu . `Referencing' with Pointers: & The & operator lets a pointer refer to (`point to')ANY variable of the same base type: type int int int i=2,j=3; // variety of integers list[4] = {1,6,4,8}; *pNow, *p1, *p2; // integer pointers p1 = &i; // p1 refers to i pNow = list+1; // pNow[0] == list[1] p2 = 1+ &(list[2]); // p2 refers to list[3] printf("%d, %d, %d\n",pNow[0], p1[0], p2[0]); Result: > 6, 2, 8 > Note: we can reference array elements! Note: my pointer names all begin with p Pointer `De-Referencing' by p[0] Referencing: point to an ordinary variable: &var pNum = &(dist); De-referencing: find or set the value where we point: pNum[0]=3.2; (a.k.a. Indirection') Looks a bit silly "element 0 of a one-element array" double *pNum; double dist = 82.481; Result: > dist is 82.481 > size: dist->8 bytes, pNum->4 bytes > pNum = &dist; printf("dist is %f\n", pNum[0]); printf("size: dist->%d bytes,pNum-> %d bytes\n", sizeof(dist), sizeof(pNum)); DANGER! `De-Referencing' by *p Referencing: point to an ordinary variable: &var pNum = &(dist); De-referencing: find/set the value where we point: pNum[0]=3.2; (a.k.a. Indirection') Looks a bit silly "element 0 of a one-element array" Alternative: use the `dereferencing' operator: *var !!DON'T CONFUSE WITH DECLARATION!!! double *pNum; double dist = 82.481; Result: > dist is 82.481 or 82.481 or 82.481 > size: dist->8 bytes, pNum->4 bytes > pNum = &dist; printf("dist is %lf or %lf or %lf\n",dist,pNum[0],*pNum); printf("size: dist->%d bytes,pNum-> %d bytes\n", sizeof(dist), sizeof(pNum)); Reference, De-reference Referencing: address-of operator: ptr = &var; Indirection I: by array index: var = ptr[0]; Indirection II: by * operator: var = *ptr; CAREFUL: Never confuse pointer declaration with indirection operator. Address-of, indirection operators CANCEL: *&&* does nothing. Address-of, indirection operator precedence is BELOW +,-, ++, --; Know how to read (BUT NEVER USE!) ugly expressions like these: USE! i=*pDigit++; (*pDigit)++; I =--(*pDigit++); int y2k[4] = {2,0,0,2}; int *pDigit, i=0; pDigit = y2k; i = *pDigit++; // what did we do? printf("%d, %d\n",i, *pDigit); y2k: 2,0,0,2; i=0 Pointers: Why Bother? Why are pointers any better than an array index? Because: One pointer can select from many arrays Pointers can `point to' (hold the address of) much more than just array elements: --such as- Any array or an array element; Any variable; (!) Any function; (!!) point to other pointers:(!!!) pointers can link together complex webs of data (later, with structures) GRAPHS of IDEAS! ! Pointers & Functions: Example 0 void swapNO(int a, int b); int main (void) { int x=10, y=25; // How can int *px, *py; // declare //fcn. prototype we SWAP these 2 integers? 2 integer pointers } px = &x; // px points to x... py = &y; printf("x=%d,y=%d\n",x,y); swapNO(x, y); //TRY TO swap values x, y printf("x=%d,y=%d\n",x,y); return 0; Result: void swapNO(int a, int b) { int tmp; tmp a b } = = = a; b; tmp; > x=10,y=25 > x=10,y=25 WHY DOES THIS FAIL? the function's formal parameters a,b (local variables in the function) are declared as int; int COPIED FROM the arguments x,y Pointers & Functions: Example I void swap1(int pa, int main (void) { int x=10, y=25; // int *px, *py; // int pb); // fcn. prototype How can we SWAP these 2 integers? declare 2 integer pointers px = &x; // px points to x... py = &y; printf("x=%d,y=%d\n",x,y); swap1(px, py); // swap values POINTED TO BY px,py printf("x=%d,y=%d\n",x,y); return 0; } void swap1(int pa, int pb) { int tmp; tmp pa[0] pb[0] } = = = pa[0]; pb[0]; tmp; Result: > x=10,y=25 > x=25,y=10 the function's formal parameters (local variables in the function) are declared as int array names. De-reference using array elements. Pointers & Functions: Example II void swap2(int *pa, int *pb); // fcn. prototype int main (void) { int x=10, y=25; // How can we SWAP these 2 integers? int *px, *py; // declare 2 integer pointers px = &x; // px points to x... py = &y; printf("x=%d,y=%d\n",x,y); swap2(px, py); // swap values POINTED TO BY px,py printf("x=%d,y=%d\n",x,y); return 0; } void swap2(int *pa, int *pb) { int tmp; tmp pa[0] pb[0] } = = = pa[0]; pb[0]; tmp; Result: > x=10,y=25 > x=25,y=10 the function's formal parameters (local variables in the function) are declared as int pointers. pointers De-reference using array elements. Pointers & Functions: Example III void swap3(int *pa, int *pb); // fcn. prototype int main (void) { int x=10, y=25; // How can we SWAP these 2 integers? int *px, *py; // declare 2 integer pointers px = &x; // px points to x... py = &y; printf("x=%d,y=%d\n",x,y); swap3(px, py); // swap values POINTED TO BY px,py printf("x=%d,y=%d\n",x,y); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp *pa *pb } = = = *pa; *pb; tmp; Result: > x=10,y=25 > x=25,y=10 the function's formal parameters (local variables in the function) are declared as int pointers. pointers De-reference using indirection op. Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; py = &y; swap3(px, py); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 908: 912: 916: 920: 924: 10 25 Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; py = &y; swap3(px, py); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 10 25 908: px: ??? 912: py: ??? 916: 920: 924: Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; py = &y; swap3(px, py); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 10 25 908: px: 900 912: py: ??? 916: 920: 924: Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; py = &y; swap3(px, py); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 10 25 908: px: 900 912: py: 904 916: 920: 924: Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; py = &y; swap3(px, py); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 10 25 908: px: 900 912: py: 916: pa: 904 900 PY PY CO CO PY PY CO CO 920: pb: 904 924: Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; py = &y; swap3(px, py); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 10 25 908: px: 900 912: py: 916: pa: 904 900 920: pb: 904 924: tmp:??? Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; py = &y; swap3(px, py); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 10 25 908: px: 900 912: py: 916: pa: 904 900 920: pb: 904 924: tmp: 10 Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; py = &y; swap3(px, py); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 25 25 908: px: 900 912: py: 916: pa: 904 900 920: pb: 904 924: tmp: 10 Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; py = &y; swap3(px, py); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 25 10 908: px: 900 912: py: 916: pa: 904 900 920: pb: 904 924: tmp: 10 Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; py = &y; swap3(px, py); return 0; } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 25 10 908: px: 900 912: py: 904 916: 920: 924: Pointers and Functions Even though the swap3() function has no return value, it has a useful side effect; it changes values at the given pointers Actual arguments to swap3() are pointers px and py that hold x and y addresses. Idea! Why not use `address-of' operator? No need for separate pointer variables px, py: instead, just pass the addresses of py x and y to swap3(x): swap3(x) Pointers & Functions: Example III void my_swap3(int *pa, int *pb); int main (void) { int x=10, y=25; int *px, *py; px = &x; Yes! py = &y; swap3(&x, &y); Simpler, and return 0; it works. } void swap3(int *pa, int *pb) { int tmp; tmp = *pa; *pa = *pb; *pb = tmp; } Computer Memory address var name: value 900: x: 904: y: 908: 912: 916: 920: 924: 10 25 ...
View Full Document

This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.

Ask a homework question - tutors are online