To view the original article, please visit: Http://blog.csdn.net/xingjiarong
In C, when the pointer operator and + + or-combined can easily be confused in the order of operations, here is a summary of the following 6 combination: * p++, (* p) ++,* (p++), ++* p,++ (* p), * (++P).
First look at the snippet code and output:
#include <stdio.h> int main () {int a[3]={1,3,5};
int *p=a;
printf ("----------------1----------------\ n");
printf ("%d\n", *p++);
printf ("%d\n", *p);
int i;
for (i=0;i<3;i++) printf ("%d", a[i]);
printf ("\ n");
printf ("----------------2----------------\ n");
P=a;//reset data printf ("%d\n", (*p) + +);
printf ("%d\n", *p);
for (i=0;i<3;i++) printf ("%d", a[i]);
printf ("\ n");
printf ("----------------3----------------\ n");
A[0]=1;//reset data p=a;
printf ("%d\n", * (p++));
printf ("%d\n", *p);
for (i=0;i<3;i++) printf ("%d", a[i]);
printf ("\ n");
printf ("----------------4----------------\ n");
P=a;
printf ("%d\n", ++*p);
printf ("%d\n", *p);
for (i=0;i<3;i++) printf ("%d", a[i]);
printf ("\ n");
printf ("----------------5----------------\ n");
P=a;
A[0]=1;
printf ("%d\n", + + (*p));
printf ("%d\n", *p); for (i=0;i<3;i++) printf ("%d", a[i]);
printf ("\ n");
printf ("----------------6----------------\ n");
P=a;
A[0]=1;
printf ("%d\n", * (++p));
printf ("%d\n", *p);
for (i=0;i<3;i++) printf ("%d", a[i]);
printf ("\ n");
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18--19 20---21 22--23 24 25 26 27 28 29-30 31 32 33 5 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
The output is like this:
The first group: *p++, its order of operation is to return the value of *p first, then P + +.
The second group: (*p) + +, his order of operation is to return the value of *p first, then the value of *p and then + +, this point from the operation of the value of the array a can be seen.
Group III: * (p++), the Order of operations is to return the value of *p, and then p + +, that is, it and *p++ the same order of operations.
All three groups return the value of *p first, the difference is in the end is p++ or *p value + +.
Fourth group: ++*p, first the value of *p + +, and then return *p value.
Fifth Group: + + (*p), first the value of *p + +, and then return *p value, so it and + + *p is the same.
Group Sixth: * (++P), first the value of P + +, and then return the value of * p, and *++p is equivalent.
The characteristics of these three groups is the final return *p value, the difference is whether *p first + + or p++.