Start slowly when going through it. First let's turn it into a program:
Code:
#include <stdio.h>
int main(void)
{
char a[ ] = "cba";
char *p = a;
printf("*p = %c\n",*p);
++*p;
printf("*p = %c\n",*p);
return 0;
}
Output:
So why?
Basically you have an array of chars that are like this in memory:
Code:
'c', 'b', 'a', '\0'
You set p to point to this array. That means p holds the address of a, and can be treated in some ways as if it's a.
Arrays are kind of special when you consider how the relationship they share with pointers. When you use an array name, you're usually referencing a pointer to the first element of the array. This is important. Pointing p to a actually points p at the first element of a.
So what does ++*p do?
First of all, it takes p, and dereferences it, and then performs the ++ on whatever the result was. This is equivalent of typing this out:
This means that you're grabbing where p points at and altering what it points at to be one more than it used to be.
Since p points at the memory location of letter c inside the array, you end up ++ing the letter c. This causes c to be d, and the array a to look like this in memory:
Code:
'd', 'b', 'a', '\0'
Hope this helps.
Edit: Oh, well, beaten again. lol..