I tried to execute the following piece of code,
int val=10;
printf("%d",++val++);
As expected I got the following error message, "lvalue required as increment operand
". But when I made the following change the program was working fine.
int val=10;
int *ptr=&val;
printf("%d",++*ptr++);
The program gave an output of 11. The output value was not surprising.
But the whole construct ++*ptr++
being an lvalue
and not an rvalue
was confusing.
I printed the contents of ptr
before and after as follows,
printf("%u\n",ptr);
printf("%d",++*ptr++);
printf("\n%d\n",ptr);
2293532 and 2293536 were the addresses printed on screen. So clearly ptr
has incremented and pointer addition is involved.
The output makes sense except for the fact that *ptr
retrieves the content of the variable whose address is stored in ptr
which is 10 and ++*ptr
increments it to 11. This definitely is an rvalue
. But the post increment (++
) has been bound to ptr
I believe. Why so? What rules govern such bindings in C?