#include <stdio.h>
int main()
{
char a[] = "hello";
char *ptr = a;
printf ("%c\n",*ptr++);//it prints character 'h'.
printf ("%c\n",*ptr);//it prints character 'e'.
return 0;
}
As I understand it: In the above code, in *ptr++ expression, both * and ++ have same precedence and operation will take place from right to left, which means pointer will increment first and deference will happen next. So it should print the character 'e' in the first printf statement. But it is not.
So my question is: Where will it store the incremented value (in, *ptr++) if it is not dereferencing that location in first printf statement?
ptr++means “incrementptr, but return the pre-increment value.”Thus despite the fact that the increment happens first, it is the original, non-incremented pointer that is being dereferenced.
By contrast, if your precedence reasoning is correct,
*++ptrshould printeas you expect.++ptrmeans “incrementptrand return the post-increment value”.