First let's simplify your program into something that's comparable:
char arr[] = "geeksforgeeks";
char* p = arr;
*p++;
printf("%s %c\n", arr, *p);
char* ptr = arr;
++*ptr++;
printf("%s %c\n", arr, *ptr);
Next lets look at the operator precedence and hypothesize what we expect to happen:
- Postfix increment (Happens 1st because it has the highest precedence of the three operators)
- Dereference (Happens 2nd because operators of precedence 2 associate Right-to-left)
- Prefix increment (Happens last because it is the leftmost operator of the lowest precedence)
So we expect *p++
to have no effect, other than advancing p
to the 2nd position because:
- Postfix increment
p
- Dereference the address of
p
without the increment in address
And we expect ++*ptr++
to increment the current character and advance ptr
to the 2nd position because:
- Postfix increment
ptr
- Dereference the address of
ptr
without the increment in address
- Increment the value of the
char
at the address ptr
And by looking at this Live Example you can see that our hypothesis was correct, we even get a warning that:
Value computed is not used [-Wunused-value] *p++
Our results are:
geeksforgeeks e
heeksforgeeks e