Can someone explain to me why postfix and prefix increment of pointer to pointer works differently, please?
My code is as follow:
#include %26lt;stdio.h%26gt;
int main()
{
char* s[ ] = {"hello", "how", "are", "you"};
char ** p;
p= s;
printf("%s ", *p); //print out hello expected
*p++;
printf("%s", *p); //print out how as expected
++*p;
printf("%s", *p); //print out "ow". expecting "are"
}
My question is if I *p++, p points to next string in the array. But when I do *p++, it points to the next word of the string it is pointing to. Why??
Thank you very much in advance.
Incrementing array of pointers in C?
The problem does not lie in preincrement or postincrement
The problem lies in operator precedence.
p=s =%26gt; *p = s[0]
Now *p++ resolves to *(p++) = s[1]
Then ++*p resolves to ++(*p) = s[1][1]
What I think you needed was *(++p) = s[2]
Its always advised to use brackets to ensure that you clearly understand what precedence you are expecting.
Hope this helps.
gladiolus
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment