[Solved] Pointer char output explanation


OK, I will try to explain this as simply as I can.
When you do: putchar(*pch++) what you say is ‘print that character of the address that pch points to and then increment the pch to point to the next address’. Essentially, before the first putchar(), *pch=”a” and after *pch=”b”(because pch points now to ch[1]).

Now, in the second putchar() what you say is: ‘print the character in the address that pch points to and then increment the value of the CHARACTER, in that address, by 1’. So, instead of doing what you did in the second putchar(), you could replace this line whith these two lines:
putchar(*pch); // 'b'
*pch += 1; // see, you increment the value(notice the *), not the pointer.

However, because as I said, it increments the character AFTER the second putchar(), it just prints what it was which is ‘b’. So, just to clear, after the second putchar(), ch[0] = ‘a'(we did not change anything in this value)
and ch[1] = ‘c'(we incremented the value of the character by 1 and ‘b’ + 1 = ‘c’). The rest is unchanged, so ch[2] = ‘c’, ch[3] = ‘d’ and so on.. but pch points to ch[1]
Now, in the third putchar(), you do something similar with the first except that you first increment the address that pch points and then print the value of this address.So, you could replace the third putchar() with these two lines:

pch++; // increment the pointer by one, it now points to ch[2], which is 'c'(it remained unchanged)
putchar(*pch); // 'c'

So, after the third putchar(), ch[0] = ‘a'(remained unchanged), ch[1] = ‘c'(changed in the second putchar()), ch[2] = ‘c'(remained unchanged)

Finally, in the last putchar(), what you do is similar to the second except that you increment the value of the CHARACTER that pch points to before you print it.
Remember that pch points to ch[2]
So, you could replace the last putchar() with these two lines:

*pch += 1; // 'c' + 1 = 'd'
putchar(*pch); // 'd'

So, after the 4 putchars, ch[0] = ‘a'(it remained unchanged), ch[1] = ‘c'(changed in the second putchar()), ch[2] = ‘d'(changed in the fourth putchar()) and ch[3] = ‘d'(it remained unchanged) and the rest of the string remains unchanged, and I think it is now obvious why you get the result you get from printf().

solved Pointer char output explanation