In the C language, when the pointer operator and + + or – combine to easily divide the order of operations, summarized here, the following is a total of analysis 6 combinations: * p++, (* p) ++,* (p++), ++* p,++ (* p), * (++P).
First look at the segment code and output:
#include <stdio.h>intMain () {inta[3]={1,3,5};int *p=a;printf("----------------1----------------\ n");printf("%d\ n",*p++);printf("%d\ n",*p);intI for(i=0;i<3; i++)printf("%d ", A[i]);printf("\ n");printf("----------------2----------------\ n"); P=a;//resetDataprintf("%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;//resetData 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;}
The output is this:
The first group: *p++, whose order of operations is to return the value of *p first, and then P + +.
Second group: (*p) + +, his order of operations is to return the value of *p first, and then the value of *p + +, which is seen from the value of array a after the operation.
The third group: * (p++), the Order of operations is to return the value of *p first, and then P + +, that is, it is the same as the *p++ order of operations.
All three groups return the value of *p first, the difference is whether it is the value of p++ or *p + +.
Group Fourth: ++*p, the value of *p is first + +, and then the value of *p is returned.
Group Fifth: + + (*p), the value of *p is first + +, and then return the value of *p, so it is the same as the + + *p.
Group Sixth: * (++P), first the value of P + +, and then return the value of * p, and *++p is equivalent.
The three groups are characterized by the final return of the value of *p, the difference is whether it is *p first + + or p++.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The problem of operation order in the combination of pointer and increment operator in C language