Why does *p++ first assign the value to i and then increment the p pointer, even though ++ post-increment has a higher precedence, and associativity is from Right-to-Left?
int main()
{
int a[]={55,66,25,35,45};
int i;
int *p=&a;
printf("%u \n",p);
i=*p++;
printf(" %u %d",p,i);
printf("\n %d",*p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
4056 4060 55 66
The semantics of the postfix ++ operator are that the expression p++ evaluates to the current value of p, and as a side effect p is incremented. Effectively, i = *p++; is the same as
i = *p;
p++;
Run Code Online (Sandbox Code Playgroud)
The semantics of the prefix ++ operator are that the expression evaluates to the current value of p plus one, and as a side effect p is incremented. i = *++p; would effectively be the same as
i = *(p + 1);
p++;
Run Code Online (Sandbox Code Playgroud)
Precedence does not control order of evaluation - it only controls parsing (which operators are grouped with which operands).
| 归档时间: |
|
| 查看次数: |
104 次 |
| 最近记录: |