I'm currently learning about pointers in C, but I'm a little confused about how right to left associativity works with regards to incrementing a pointer versus incrementing the value being pointed at.
It is understood that ++ and * are both right to left associative in C.
I don't see how *s++
moves the array ahead 1, yet (*s)++
increments the value being pointed at but doesn't move the array ahead.
For example:
#include <stdio.h>
int main(){
char c[20] = "Help";
char *s;
s = c;
printf("init: %s\n",s); // Help
++*s;
printf("++*s: %s\n",s); // "Ielp" ascii increment of first element
++(*s);
printf("++(*s): %s\n",s); // "Jelp" ascii increment of first element
*s++;
printf("*s++: %s\n",s); // "elp" moves to next element in array
(*s)++;
printf("(*s)++: %s\n",s); // "flp" ascii increment of current element
*(++s);
printf("*(++s)\n: %s\n",s); // "lp" moves to next element
*++s;
printf("*++s\n: %s\n",s); // "p" moves to next element
return 0;
}
For the code above, can someone help explain what is going on AND why?
Thanks for taking a look.