




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





1
2
printf("y is at %x\n", &y); Square(&y); printf("y = %d\n", y); *p = 25; Square(p); printf("y = %d\n", y); printf("a[0] is at %x\n", &a[0]); printf("a[1] = %d\n", a[1]); Square(&a[1]); printf("a[1] = %d\n", a[1]); printf("Integers have size %d\n", sizeof(int)); return 0; } // Square squares its argument, in place. That is, Square // assigns its argument to its own square. Note use of // simulated call-by-reference. // int Square(int *x) { printf("In Square, x points to %x\n", x); *x = *x * *x; // Set argument to its own square. return 0; }
// CS 350, Spring 2009 // Illinois Institute of Technology // J. Sasaki // Practice declaration and initializiation of pointers. #include<stdio.h> main() { int x = 2, y = 5; int *p, q; / Note that * before q is critical! / p = &x; / Make p point to x. I.e., make value of p / / be the address of x / q = &y; / Make q point to y. ??? / printf("x = %d", x); printf(", y = %d\n", y); printf("x stored at %x", &x); printf(", y stored at %x\n", &y); printf("p = %x", p); printf(", q = %x\n", q); printf("p stored at %x", &p); printf(", q stored at %x\n", &q); printf("p = %d, *q = %d\n", *p, q); p = q = &x; printf("\nAfter aliasing p and q:\n"); printf("p = %x, q = %x\n", p, q); printf("p = %x, *q = %x\n", *p, *q); *p = 17; printf("\nAfter setting *p = 17:\n"); printf("&x = %x, p = %x, q = %x\n", &x, p, q); printf("x = %d, *p = %d, *q = %d\n", x, *p, *q); p = &x; q = &y; x = y = 17; printf("\nAfter setting p = &x and q = &y and x = y = 17:\n"); printf("&x = %x, &y = %x, p = %x, q = %x\n", &x, &y, p, q); printf("x = %d, y = %d, *p = %d, *q = %d\n", x, y, *p, *q); x = 3; *q = 5; printf("\nAfter setting x = 3 and *q = 5:\n");
Notes on Pointers Page 1
CS 350, Spring 2009 CS Dept, Illinois Institute of Technology J. Sasaki
x (^17) 0x A null pointer variable. int *r; r!=!NULL; An integer variable stored at location 0x1000. Code: int x = 17; A pointer variable that points to x. int *p; p = &x; p 0x1000 q r A pointer-to-pointer variable that points to x. int **q; q = &p; 0x2400 0x 0x variable the address it points to its address (the arrow goes to the item at the address pointed to) (Actual addresses are usually unknown and omitted; some hypothetical addresses are shown above for completeness.) int a[4]; Uninitialized ???? array of integers a a[0], i.e., *a a[1], i.e., *(a+1) a[2], i.e., *(a+2) a[3], i.e., *(a+3) s (^)? int *s; // An uninitialized pointer. Note that s, s[0], s[1], etc. can cause a runtime error and are ill-advised. e[i] means (e+i), so &e[i] equals &((e+i)) equals e+i. (In general, &((an address)) equals the address.) a a+1 a+2 a+ p Note: Since the name a stands for an address, you can argue that people shouldn't represent arrays by writing the name next to the first location โ the name should point to the first location. But that's the way people do it. Call it a tradition. int *p; p = a; Make p point to a[0]. &x &p
Notes on Pointers Page 2 Code for the diagram below: int a[3] = { 10, 20, 30 }, b = 40; int (c)[4]; / array of pointers */ int *d; / or int *(d[]); */ d = &c[0]; d[0] = a+1; *(d+1) = &a[1]; c[2] = &b; c[3] = c[1] + 1; True given the above diagram: p!==!q, pโq!==!0, p < r, p+1 < r, p+2!== r, rโp!==!2, p!==!q, q!==!r, p[1]!==!18, *(p+1)!==!18, *(p+1) > *r, p-1 < p, *(p-1) > *p Some code that produces the diagram above: int a[4] = {19, 17, 18, 17}; int *p, *q, *r; p!=!q!= &a[1]; r = &a[3]; 19 17 18 17 p q r a d a 10 20 30 b 40 c 10 20 30