tags:

views:

329

answers:

6

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

A: 

The first increments the pointer, the second increments the value pointed to.

As an experiment, try this:

int main() {
    int x = 20;
    int *d = &x;
    printf("d = %p\n", d);
    int z = (*d)++;
    printf("z = %d\n", z);
    printf("d = %p\n", d);
    int y = *d++;
    printf("y = %d\n", y);
    printf("d = %p\n", d);
}
retracile
+7  A: 

The increment ++ has higher operator precedence than the dereference *, so *d++ increments the pointer d to point to the next location within the array, but the result of ++ is the original pointer d, so *d returns the original element being pointed to. Conversely, (*d)++ just increments the value being pointed to.

Example:

// Case 1
int array[2] = {1, 2};
int *d = &array[0];
int x = *d++;
assert(x == 1 && d == &array[1]);  // x gets the first element, d points to the second

// Case 2
int array[2] = {1, 2};
int *d = &array[0];
int x = (*d)++;
assert(x == 1 && d == &array[0] && array[0] == 2);
// array[0] gets incremented, d still points there, but x receives old value
Adam Rosenfield
+11  A: 

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!

AraK
Thanks, that's exactly what it does.
goe
LoooL ... I got downvoted for no reason!
AraK
Jørgen Fogh
@Jørgen: How could the creators of C write C code in a bad style? By definition, this is how it's done in C.
sbi
sbi
Johannes Schaub - litb
Also notice that `sizeof(first)` already accounts for the terminating null. No need to add 1. I think you had `strlen` in mind when writing this :)
Johannes Schaub - litb
AraK
@litb Thanks, I got confused because I wanted to use pointers first, then I decided to use arrays :)
AraK
@litb Thanks too much for your comment about the array, I am useless without a compiler :P
AraK
+1  A: 

They do return the same result, but the state change in your program is completely different.

This is easiest to understand if we just expand out the operations.

x = *d++;
// same as
x = *d;
d += 1; // remember that pointers increment by the size of the thing they point to

x = (*d)++;
// same as
x = *d;
*d += 1; // unless *d is also a pointer, this will likely really just add 1
DigitalRoss
A: 

I don't have a compiler handy.

a = (*d)++;
b = (*d);

is a==b? i don't think it is.

Dustin Getz
+1  A: 

In the official C terminology, these expressions do give you the same results, as they should. In the proper terminology, the "result" of a non-void expression is what that expression evaluates to. Both of your expressions evaluate to the initial value of *d, so not surprisingly, the results are the same.

However, an addition to a "result" every expression in C has zero or more so called "side effects". And side effects of these two expressions are completely different. The first expression increments the value of pointer 'd'. The second expression increments the value of '*d' (the pointed value).

AndreyT