int i; // An integer variable int a[10]; // An integer array int *p; // A reference variable to an int variable // p can contain an address of an int variable |
int i; // An integer variable int a[10]; // An integer array |
Illustrated:
![]() |
Notice that:
|
int *p; p = &i; *p will access i p = &a[0]; *p will access a[0] p = &a[1]; *p will access a[1] |
|
Example:
|
Example C progra:
int main(int argc, char *argv[]) { int a[10]; int *p; p = &a[0]; // p points to variable a[0] printf("p = %u, &a[%d] = %u\n", p, 0, &a[0] ); printf("p + 1 = %u, &a[%d] = %u\n", p+1, 1, &a[1] ); printf("p + 2 = %u, &a[%d] = %u\n", p+2, 2, &a[2] ); printf("p + 3 = %u, &a[%d] = %u\n", p+3, 3, &a[3] ); } |
Output of C program:
p = 4290769444, &a[0] = 4290769444 (increased by 4 !!!) p + 1 = 4290769448, &a[1] = 4290769448 p + 2 = 4290769452, &a[2] = 4290769452 p + 3 = 4290769456, &a[3] = 4290769456 |
How to run the program:
|
![]() |
In other words:
If p points to a[0] then: p + 1 = &a[0] + 4 = the address of the array element a[1] !! p + 2 = &a[0] + 8 = the address of the array element a[2] !! |
|
|
In other words:
If p points to a[5] then: p - 1 = &a[5] - 4 = the address of the array element a[4] !! p - 2 = &a[5] - 8 = the address of the array element a[3] !! |
|
|
Example:
![]() |
Explanation:
|
int main(int argc, char *argv[]) { int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 }; int *p; p = &a[0]; // p points to variable a[0] printf("*p = %d, a[%d] = %d\n", *p, 0, a[0] ); printf("*(p + 1) = %d, a[%d] = %d\n", *(p+1), 1, a[1] ); printf("*(p + 2) = %d, a[%d] = %d\n", *(p+2), 2, a[2] ); printf("*(p + 3) = %d, a[%d] = %d\n", *(p+3), 3, a[3] ); } |
Output:
*p = 11, a[0] = 11 *(p + 1) = 22, a[1] = 22 *(p + 2) = 33, a[2] = 33 *(p + 3) = 44, a[3] = 44 |
How to run the program:
|
p++ means: 1. p = p + 1 2. returns the OLD value (before the increment) in p p-- means: 1. p = p - 1 2. returns the OLD value (before the increment) in p |
if p is a reference typed variable, then: p++ means: 1. p = p + 1 and will store p + 1*sizeof( (*p) ) into p 2. returns the OLD value (before the increment) in p p-- means: 1. p = p - 1 and will store p - 1*sizeof( (*p) ) into p 2. returns the OLD value (before the increment) in p |
int main(int argc, char *argv[]) { int a[10]; int *p; p = &a[0]; // p points to variable a[0] printf("staring p = %u\n", p); p++; printf("after p++: p = %u\n", p ); p++; printf("after p++: p = %u\n", p ); p++; printf("after p++: p = %u\n", p ); } |
Output:
staring p = 4290769444 // p++ increases p by 4 = sizeof(int) !!! after p++: p = 4290769448 after p++: p = 4290769452 after p++: p = 4290769456 |
How to run the program:
|
means *(p++) <===> *( p++ ) // apply p++ first p++ : 1. p = p+1 2. returns the OLD value of p !!! <===> *( use the OLD value of p ) <===> the variable pointed to by the OLD value of p !!! |
In plain English:
|
int main(int argc, char *argv[]) { int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 }; int i; int *p; printf("Array a[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", a[i] ); putchar('\n'); p = &a[0]; // p points to variable a[0] printf("*(p++) = %d\n", *(p++) ); printf("*(p++) = %d\n", *(p++) ); printf("*(p++) = %d\n", *(p++) ); printf("*(p++) = %d\n", *(p++) ); } |
Output:
Array a[]: 11 22 33 44 55 66 77 88 99 777 *(p++) = 11 *(p++) = 22 *(p++) = 33 *(p++) = 44 |
Explanation:
|
How to run the program:
|
int main(int argc, char *argv[]) { int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 }; int b[10]; int i; for ( i = 0; i < 10; i++ ) b[i] = a[i]; printf("Array a[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", a[i] ); putchar('\n'); printf("Array b[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", b[i] ); putchar('\n'); } |
How to run the program:
|
int main(int argc, char *argv[]) { int a[10] = { 11, 22, 33, 44, 55, 66, 77, 88, 99, 777 }; int b[10]; int i; int *p, *q; // Defines 2 reference variables // Incorrect: int *p, q; - q will be an int p = &a[0]; q = &b[0]; for ( i = 0; i < 10; i++ ) *(q++) = *(p++); printf("Array a[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", a[i] ); putchar('\n'); printf("Array b[]: "); for ( i = 0; i < 10; i++ ) printf("%d ", b[i] ); putchar('\n'); } |
How to run the program:
|
|
|
|
*p++ ===> *p++ // * and ++ has same priority // ==> Use associativity rule // ==> Perform operations from right-to-left ! // ==> Perform ++ first !!! ===> *(p++) |
|
DataType * p; *p++ |
*p++ means:
|