What's the difference between *d++ and (*d)++ in C?

c, pointers

Solution

No they are not the same. Assume that `d` is a pointer to `int`:

int n = 0;
int* d = &n;

*d++; // d++ then *d, but d++ is applied after the statement.
(*d)++; // == n++, just add one to the place where d points to.

I think there is an example in K&R where we need to copy a c-string to another:

char* first = "hello world!";
char* second = malloc(strlen(first)+1);
....

while(*second++ = *first++)
{
 // nothing goes here :)
}

The code is simple, put the character pointed by `first` into the character pointed by `second`, then increment both pointers after the expression. Of course when the last character is copied which is '\0', the expression results to `false` and it stops!

Problem

as in the title, what's the difference because these two seem to get me the same results?

Original source