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:
|
#include |
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:
|