Example: Pointer Basics #include <stdio.h> int (void) { float a1, a2; /* Simple variables */ float *p1, *p2, *w; /* Pointers to variables of type float */ a1 = 10.0, a2 = 20.0; printf("after a1 = 10.0; a2 = 20.0\n"); printf(" a1=%f &a1=%ld\n", a1, &a1); printf(" a2=%f &a2=%ld\n\n", a2, &a2); p1 = &a1; p2 = &a2; printf("after p1 = &a1; p2 = &a2\n"); printf(" *p1=%f p1=%ld &p1=%ld\n", *p1, p1, &p1); printf(" *p2=%f p2=%ld &p2=%ld\n\n", *p2, p2, &p2); w = p1; printf("after w = p1\n"); printf(" *w=%f w=%ld &w=%ld\n\n", *w, w, &w); *p1 = 99.0; printf("after *p1 = 99.0\n"); printf(" *p1=%f p1=%ld &p1=%ld\n", *p1, p1, &p1); printf(" a1=%f &a1=%ld\n\n", a1, &a1); a1 = *p2 + 7; printf("after a1 = *p2 + 7\n"); printf(" a1=%f &a1=%ld\n\n", a1, &a1); *p2 = *p1 * 2; printf("after *p2 = *p1 * 2\n"); printf(" *p2=%f p2=%ld &p2=%ld\n", *p2, p2, &p2); printf(" a2=%f &a2=%ld\n\n", a2, &a2); } return 0; Actual output: After a1 = 10.0; a2 = 20.0 a1=10.000000 &a1=1012 a2=20.000000 &a2=1016 After p1 = &a1; p2 = &a2 *p1=10.000000 p1=1012 &p1=1000 *p2=20.000000 p2=1016 &p2=1004 After w = p1 *w=10.000000 w=1012 &w=1008 After *p1 = 99.0 *p1=99.000000 p1=1012 &p1=1000 a1=99.000000 &a1=1012 After a1 = *p2 + 7 a1=27.000000 &a1=1012 After *p2 = *p1 * 2 *p2=54.000000 p2=1016 &p2=1004 a2=54.000000 &a2=1016 1
Assumptions: The system allocates memory space to store the requested variables at the addresses shown below. The pointer variables are not initialized. System State: After declaring local float variables a1 and a2 and pointer variables p1, p2, and w in function () p1 1000? p2 1004? a1 1012? a2 1016? w 1008? System State: After executing a1 = 10.0; a2 = 20.0; p1 1000? p2 1004? a1 1012 10.0 w 1008? System State: After executing p1 = &a1; p2 = &a2 a1 1012 10.0 w 1008? 2
System State: After executing w = p1; a1 1012 10.0 w 1008 1012 System State: After executing *p1 = 99.0; a1 1012 99.0 w 1008 1012 System State: After executing a1 = *p2 + 7; a1 1012 27.0 w 1008 1012 3
System State: After executing *p2 = *p1 * 2; (Note: * is used here both for dereferencing and for multiplication.) a1 1012 27.0 a2 1016 54.0 w 1008 1012 4
Levels of Indirection (Pointers that reference other pointers) int x; int * p; int ** q; x = 5; p = &x; q = &p; printf("%d %d %d\n", x, *p, **q); /* Displays 5 5 5 */ *p = 4; printf("%d %d %d\n", x, *p, **q); /* Displays 4 4 4 */ **q = 3; printf("%d %d %d\n", x, *p, **q); /* Displays 3 3 3 */ p = NULL; 5
System State: After declaring integer variable x and pointer variables p and q. x 5, &p 1300, p?, *p?, &q 1500, q?, *q?, **q? q 1500? p 1300? x 1150 5 System State: After executing p = &x; x 5, &p 1300, p 1150, *p 5, &q 1500, q?, *q?, **q? q 1500? p 1300 1150 x 1150 5 System State: After executing q = &p; x 5, &p 1300, p 1150, *p 5, &q 1500, q 1300, *q 1150, **q 5 q 1500 1300 p 1300 1150 x 1150 5 System State: After executing *p = 4; x 4, &p 1300, p 1150, *p 4, &q 1500, q 1300, *q 1150, **q 4 q 1500 1300 p 1300 1150 x 1150 4 System State: After executing **q = 3; x 3, &p 1300, p 1150, *p 3, &q 1500, q 1300, *q 1150, **q 3 q 1500 1300 p 1300 1150 x 1150 3 System State: After executing p = NULL; x 3, &p 1300, p NULL, *p? 1, &q 1500, q 1300, *q NULL, **q? 1 q 1500 1300 p 1300 NULL x 1150 3 1 Trying to dereference a NULL pointer will cause a memory fault error during program execution. 6
Pointer Arithmetic #include <stdio.h> #define SIZE 3 int (void) { double x[size] = {-1.0, 5.0, 9.8}; double * p; int i; printf("output x[i]\n"); for (i = 0; i < SIZE; i++) printf(" x=%ld, i=%d: %6.2f\n", x, i, x[i]); p = x; printf("\noutput p[i]\n"); for (i = 0; i < SIZE; i++) printf(" p=%ld, i=%d: %6.2f\n", p, i, p[i]); printf("\noutput *(p+i) loop\n"); for (i = 0; i < SIZE; i++) printf(" p+i=%ld: %6.2f\n", p+i, *(p+i)); printf("\noutput *p++ loop\n"); for (i = 0; i < SIZE; i++) printf(" p=%ld: %6.2f\n", p, *p++); } printf("\n"); return 0; Actual output: Output x[i] x=2000, i=0: -1.00 x=2000, i=1: 5.00 x=2000, i=2: 9.80 Output p[i] p=2000, i=0: -1.00 p=2000, i=1: 5.00 p=2000, i=2: 9.80 Output *(p+i) loop p+i=2000: -1.00 p+i=2008: 5.00 p+i=2016: 9.80 Output *p++ loop p=2000: -1.00 p=2008: 5.00 p=2016: 9.80 After the *p++ loop is done, the value of p has changed and is no longer equal to x. 7
What happens when a pointer variable is incremented using p++? Notes: *(p+0) *(p-0) *(p) *p Name Contents x 2000 p 1000 2000 Address Equivalent access methods Contents 2000 x[0] p[0] *(p+0) -1.0 2008 x[1] p[1] *(p+1) 5.0 2016 x[2] p[2] *(p+2) 9.8 2024? Name Contents x 2000 p 1000 2008 Address Equivalent access methods Contents 2000 x[0] *(p-1) -1.0 2008 x[1] p[0] *(p+0) 5.0 2016 x[2] p[1] *(p+1) 9.8 2024? Name Contents x 2000 p 1000 2016 Address Equivalent access methods Contents 2000 x[0] *(p-2) -1.0 2008 x[1] *(p-1) 5.0 2016 x[2] p[0] *(p+0) 9.8 2024? 8