# "*x + *x++" Address/Pointer "?"

This is a discussion on "*x + *x++" Address/Pointer "?" within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { int i = 5; printf("&#37;d", g(&i)); } int g (int *x) { return (*x ...

1. ## "*x + *x++" Address/Pointer "?"

Code:
```#include <stdio.h>

int main(void)
{
int i = 5;
printf("&#37;d", g(&i));
}

int g (int *x)
{
return (*x + *x++);
}```
10 is printed out.

But if you change the return statement to:

Code:
`return (*x + *x+=1);`
11 is printed out.

Can someone explain to me why "++" before or after *x doesn't work and why "+=1" does?

I always thought "++" and "+=1" were the same thing when used after a variable.

2. *x++ increments x, not *x.

However, I believe that undefined behaviour is involved in both cases. You are reading a variable twice between consecutive sequence points where the variable is modified. To avoid this, perhaps you should implement g as:
Code:
```int g(int *x)
{
int t = (*x)++;
return *x + t;
}```

3. ++*x does work just like *x += 1, and t = *x++

++a isn't the same as a++

*x + *x++ means you add 1 to 5 and don't save, then dereference x to get 5 + 5 = 10

*x + ++*x means you add 1 to 5 and save the change back to x, then dereference x to get 6 + 6 = 12

*x + (*x += 1) is the same thing, 6 + 6 = 12

Someone can explain that in technical terms.

4. > *x + *x++ means you add 1 to 5 and don't save, then dereference x to get 5 + 5 = 10
No it doesn't. ++ has greater precedence than *.

You dereference, then add 1 to the address of x. For example:

Code:
```[zac@neux ~]\$ gcc -std=c89 -pedantic -Wall eg.c -o eg
[zac@neux ~]\$ cat eg.c
#include <stdio.h>

int main(void)
{
int array[2] = {5, 10};
int * x = array;

printf("&#37;d\n", *(x++));
printf("%d\n", *x);
return 0;
}
[zac@neux ~]\$ ./eg
5
10```

5. Originally Posted by Dae
++*x does work just like *x += 1, and t = *x++
You made the same mistake as Marth_01. Where the expression stands alone, ++*x means the same thing as (*x)++ and *x += 1. It is not the same as *x++, which increments x, not *x.

6. This is where a operator precedence table comes in handy . This can all be avoided with use of parenthesis though. *x++ is far easier to read as *(x++)

I'm not sure that *x + *(x++) is undefined (in this case). Since the increment of x is delayed until the end of the statement, after *x has been dereferenced.

7. Originally Posted by zacs7
*x++ is far easier to read as *(x++)
True. On the other hand, because of the famous while (*x++ = *y++) ; string copying code, the behaviour of *x++ should be well known to the average C programmer.

Originally Posted by zacs7
I'm not sure that *x + *(x++) is undefined. Since the increment of x is delayed until the end of the statement, after *x has been dereferenced.
Consider C99 section 6.5 paragraph 2:
"Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored." (Emphasis mine.)

The first use of x does not determine the value to be stored. It determines the pointer to be dereferenced to be used in a different calculation. Consequently, there is undefined behaviour. C99 provides a concrete example when it states that that paragraph renders undefined this expression:
Code:
`a[i++] = i;`

8. Originally Posted by laserlight
You made the same mistake as Marth_01. Where the expression stands alone, ++*x means the same thing as (*x)++ and *x += 1. It is not the same as *x++, which increments x, not *x.
Oh right, I wasn't thinking. That's obvious precedence. I need to use pointers more and be quiet.

9. Interesting, thanks for the research laserlight. I suppose the concrete example is pretty much what's going on here.

Is then:
Code:
```int x = 5;
int d = x++ + x++;```
Defined? Since the sequence points do not matter (As far as I can tell).

10. No, since the stored value of x is modified twice within consecutive sequence points (plus it has the same problem as x + x++ or x++ + x). Consider also that the resulting value of x could reasonably be 6 or 7, depending on your point of view.

11. I see. 'stored' was the word I was overlooking, thanks :-).