Take int ptr={10,20,30,40,50}
I understand that
print("%d", *ptr++);
in such a statement evaluation of operators is from right to left.
Hence in *ptr++ the ++ will get evaluated first and then ptr and then *
So to confirm the same I wrote a program
#include<stdio.h>
int main()
{
int array[] = { 10, 20, 30, 40, 50 };
int *q1 = array;
printf("q1 = %p\n",q1);
printf("*q1++ = %d\n",*q1++);
printf("q1 = %p\n",q1);
printf("*q1++ = %d\n",*q1);
}
The output of above program is different than the expected operator precedence by above logic.
The output I got is
q1 = 0x7ffffcff02e0
*q1++ = 10
q1 = 0x7ffffcff02e4
*q1++ = 20
but I was expecting
q1 = 0x7ffffcff02e0
*q1++ = 20
q1 = 0x7ffffcff02e4
*q1++ = 20
so did the operator precedence not happened right to left?
Or there is some thing wrong in my understanding?
UPDATE
Now here is the thing.Even if I put these brackets as mentioned so that *(ptr++) gets executed the output does not change here is new code
#include<stdio.h>
int main()
{
int array[] = { 10, 20, 30, 40, 50 };
int *q1 = array;
printf("q1 = %p\n",q1);
printf("*q1++ = %d\n",*(q1++));// note the braces here *(q1++) so that () get evaluated
printf("q1 = %p\n",q1);
printf("*q1++ = %d\n",*q1);
}
The result is still the same note the use of braces as you mentioned.
Still the output is
q1 = 0x7fff043f2120
*q1++ = 10 <-- I expected *q1++ = 20//since I used braces ()
q1 = 0x7fff043f2124
*q1++ = 20
So even after I used braces *(ptr++) that operation ++ was still executed after the current line was executed.So did curly brace () not work? Or it was not given preferences over post increment thing?
++on the right returns the value before it gets evaluated.is the same as