Output ++ * p ++

Can someone explain the result to me.

#include<stdio.h> int main() { int a[]={10,20,30}; int *p=a; ++*p++; printf("%d %d %d %d",*p,a[0],a[1],a[2]); } 

- 20 11 20 30

Post-increment has a higher priority, so the value of the second index should be increased. Why is the value of the first index increasing?

+6
source share
2 answers

Due to the priority of the operator ,

++*p++ same as ++(*(p++)) .

This is equivalent to:

 int* p1 = p++; // p1 points to a[0], p points to a[1] ++(*p1); // Increments a[0]. It is now 11. 

This explains the conclusion.

+14
source

This is because the postfix operator returns a value before the increment. Thus, the pointer grows well, but the prefix operator still applies to the original value of the pointer.

+2
source

All Articles