I cannot figure this out. Perhaps it is because it's 2am. At any rate, I am at a loss here.
#include <stdio.h>
int main()
{
char array[] = "123456789";
char* ptr = array;
printf("%c\n", *(ptr++));
printf("%c\n", *ptr);
*ptr = array[3];
printf("%c\n", *(ptr++));
printf("%c\n\n", *ptr);
return 0;
}
The result is:
1
2
4
3
I have a pointer, which I assign to
array
.I then print, what I thought would be the first index (
'2'
), but instead get1
. -- So, I assume that*(ptr++)
actually dereferences, before it increments the pointers.Then I reassign
ptr
the 4th index ('4'
) and repeat step 2. This works as expected now that I see C does not calculate the parenthesis first before dereferencing.Then I print the newly incremented
ptr
to display ('5'
) ... and I get3
?
How is that, step 1&2 and 3&4 are identical, but I get different results?