I am afraid I did not get correctly the precedence between operators. The following program works differently than expected:
#include<stdio.h>
int main(){
char a[] = "abc";
char *p;
int i;
p = a;
for(i = 0; i < 3; ++i) /* First case */
printf("%c\n", *p++);
printf("a = %s\n", a);
p = a;
for(i = 0; i < 3; ++i) /* Second case */
printf("%c\n", (*p)++);
printf("a = %s\n", a);
return 0;
}
First case: according to this table, *p++
is *(p++)
. I increment the address pointed by p
, then I dereference it. Why it returns abc
, then, and not bc + "value beyond string"
?
I encounter a similar problem with the second case, with the expression (*p)++
. In this case, the parentheses make me increase the dereferentiated value of p. Hence, I expect the expression to return bcd
rather than dbc
.