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

前端 未结 4 1922
Happy的楠姐
Happy的楠姐 2021-01-02 23:07
int val = 5;

printf(\"%d\",++val++); //gives compilation error : \'++\' needs l-value

int *p = &val;
printf(\"%d\",++*p++); //no error

Could

4条回答
  •  萌比男神i
    2021-01-02 23:38

    The expression ++val++ is the same as (++val)++ (or perhaps ++(val++), anyway it's not very relevant). The result of the ++ operator is not the variable, but the value, and you can't apply the operator to a value.

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

提交回复
热议问题