Explanation ++ val ++ and ++ * p ++ in C

int val = 5; printf("%d",++val++); //gives compilation error : '++' needs l-value int *p = &val; printf("%d",++*p++); //no error 

Can anyone explain these two cases? Thanks.

+6
c increment order-of-evaluation
source share
4 answers

++val++ same as ++(val++) . Since the result of val++ not an lvalue value, this is illegal. And as Stephen Canon noted, if the result of val++ was lvalue, ++(val++) would be undefined, since there is no sequence point between ++ s.

++*p++ same as ++(*(p++)) . Since the result of *(p++) is an lvalue, this is legal.

+27
source share

The expression ++val++ matches (++val)++ (or possibly ++(val++) , in any case, this is not very important). The result of the ++ operator is not a variable, but a value, and you cannot apply the operator to a value.

The expression ++*p++ matches ++(*(p++)) . The result of p++ is a value, but the result of *(p++) is a memory cell to which the ++ operator can be applied.

+4
source share

also note that you change the address of the pointer to

 int k = ++*p++; 
+1
source share

int j = ++val++; //gives compilation error

This is because you cannot pre-increment rvalue . ++val++ interpreted as ++(val++) because the post-increment operator has higher priority than the pre-increment operator. val++ returns rvalue , and the pre-increment operator requires its operand to be lvalue . :)

int k = ++*p++; //no error

++*p++ interpreted as ++(*(p++)) , which is quite true.

0
source share

All Articles