C language Miscellaneous (2) combination of auto-increment operators ++ and indirect access operators * and application modes, miscellaneous Operators
The auto-incrementing operator ++ has two prefixes and suffixes. When used together with the indirect access operator *, it is easy to misunderstand because of the influence of order, Parentheses, and binding relationships, this article will analyze in detail the different combinations of these operators.
++, --, And * priority order
In the priority order of the C-language operators, the combination of ++ with the suffix and -- operator operation Priority 16 is left to right; introduction: access operator *, prefix ++, and -- operator operation Priority 15. The link is from right to left. Based on this relationship, you can analyze the applications in different situations. The following examples are provided to better reflect the situation.
Example
There are arrays a [5] with values 10, 11, 12, 13, and 14 respectively. There is a pointer p pointing to a [0], and there are pointers q and integer B. Note that the values of array a and the p pointer will return to the initial state before each code segment starts. (When writing the test code at the beginning, ignoring the problem of replying to the initial value leads to many strange errors. For example, p no longer points to a [0], or a [0] has changed .)
1. suffix ++
p = a;q = p++;
P points to a [1] value is 11, and q points to a [0] value is 10. The suffix auto-increment operator returns the value before auto-increment.
2. prefix ++
p = a;q = ++p;
P points to a [1] value is 11, q points to a [1] value is 11. The prefix auto-increment operator returns the auto-increment value.
3. * p ++
p = a;b = *p++;
P points to 11 and B is 10. In this case, execute p ++, p auto-increment, and point to a [1]. The value of p ++ is the value before auto increment, that is, the address of a [0]. The value of a [0] is assigned to B.
4. * (p ++)
p = a;b = *(p++);
The result is the same as 3. * The priority is the same as that of the ++ operator. The operation order is from right to left.
5. (* p) ++
p = a;b = (*p)++;
At this time, although the result is the same as 3, the p point is obtained first, that is, a [0], a [0] auto-increment, that is, the value of a [0] is changed to 11, if auto-increment is a suffix, the value before auto-increment is returned, that is, 10. The results are the same, but the principles are quite different.
6. * ++ p
p = a;b = *++p;
P points to 11, and B points to 11. Execute ++ p and p auto-increment, point to a [1], and the value of ++ p is the auto-increment value, that is, the address of a [1, the value of a [1] is assigned to B.
7. * (++ p)
p = a;b = *(++p);
The result is the same as that of result 6. * has the same priority as that of the ++ operator. The operation order is from right to left.
8. ++ (* p)
p = a;b = ++(*p);
P points to 11, B value is 11, first take p points to a [0] value is 10, a [0] value from auto increment to 11, the returned value of prefix auto-increment is 11 to B.
9. ++ * p
p = a;b = ++*p;
The result is 8. * The priority is the same as that of the ++ operator. The operation order is from right to left.
Summary
The first principle is that after the prefix value is changed, the suffix value is before the change. The second principle is that * ++ has the same priority and is read from right to left. I have tried to sum up these points. If you have any mistakes, please correct them and hope to help those who have doubts.
Complete code
/ * This program is used to test the prefix increment operator, postfix increment operator, the priority of the operation taking the content symbol and the effect of the return value brought by the order * /
#include "stdio.h"
void myPrint (int n, int j, int k)
{
printf ("no.% d: p->% d, q->% d \ n", n, j, k);
}
void myPrintNew (int n, int j, int k)
{
printf ("no.% d: p->% d, b->% d \ n", n, j, k);
}
void init (int a [5])
{
a [0] = 10;
a [1] = 11;
a [2] = 12;
a [3] = 13;
a [4] = 14;
} // Prevent certain test operations from changing the value of the array. Use the initialization function before each use of the array
int main (int argc, char * argv)
{
// int a [5] = {10, 11, 12, 13, 14}, * p, * q, b;
int a [5], * p, * q, b;
init (a);
//1.
init (a);
p = a;
q = p ++;
myPrint (1, * p, * q);
// p points to 11, q points to 10
// Suffix auto-increment operator, which returns the value before auto-increment
//2.
init (a);
p = a;
q = ++ p;
myPrint (2, * p, * q);
// p points to 11, q points to 11
// Prefix auto-increment operator, which returns the value after auto-increment
// 1,2: reading order: left to right
// 3.
init (a);
p = a;
b = * p ++;
myPrintNew (3, * p, b);
// p points to 11, b is 10
// First execute p ++, p increments to point to a [1], the value of p ++ is the value before increment (see 1), which is the address of a [0], and the value of a [0] is assigned to b
// 4.
init (a);
p = a;
b = * (p ++);
myPrintNew (4, * p, b);
// result is the same as 3
// * has the same priority as the ++ operator, and the operation order is from right to left
// 5.
init (a);
p = a;
b = (* p) ++;
myPrintNew (5, * p, b);
// The result is the same as 3, but at this time, the value pointed to by p is first a [0], a [0] is incremented, that is, the value of a [0] becomes 11, and the increment is suffixed. Value, which is 10
// Compare with 8
// 6.
init (a);
p = a;
b = * ++ p;
myPrintNew (6, * p, b);
// p points to 11, b is 11
// Execute ++ p first, p increments to point to a [1], the value of ++ p is incremented (see 2), that is, the address of a [1] and the value of a [1] b
// 7.
init (a);
p = a;
b = * (++ p);
myPrintNew (7, * p, b);
// Same result as 6
// * has the same priority as the ++ operator, and the operation order is from right to left
//8
init (a);
p = a;
b = ++ (* p);
myPrintNew (8, * p, b);
// p points to 11, b is 11
// First take p to point to a [0] value of 10, the value of a [0] increments to 11 and the prefix increment value returns 11 to b
//9??
init (a);
p = a;
b = ++ * p;
myPrintNew (9, * p, b);
// result is the same as 8
// * has the same priority as the ++ operator, and the operation order is from right to left
return 0;
}
Running result