0
int main()
{
    int x = 30, *y, *z;
    y = &x; 
    /* Assume address of x is 500 and integer is 4 byte size */
    z = y;
    *y++ = *z++;
    x++;
    printf("x=%d, y=%d, z=%d\n", x, y, z);
    return 0;
}

The above is the code. The output is: x=31, y=504, z=504

Please correct me if I'm wrong:

From what I understand: y=&x; assigns the address of x to y. So, y now holds the value of 500.

z=y; Since y = 500, this assigns 500 to z.

What really confuses me is this part *y++=*z++;, I don't exactly know what this means as there's many things going on at the same time. z gets incremented and pointed to somewhere (AND where is it pointing actually? There is no address assigned to it like y i.e y=&x;. Then *y also gets incremented at the same time (are you even allowed to do that?).

Another thing that confuses me is that: in my opinion, since y points to x, when y++ happens, x should be incremented to 31, and then when going down the code block x++ happens, x should now be 32.

So, question is, how did we get that output of x=31, y=504, z=504? Thank you.

13
  • 5
    printf("x=%d, y=%d, z=%d\n", x, y, z); is undefined behavior. If you absolutely have to, use %p to print pointers. Commented Jul 17, 2017 at 6:22
  • 5
    Possible duplicate of difference between *y++ and ++*y? Commented Jul 17, 2017 at 6:23
  • @Evert Yes, it is for an exercise. I'm trying to understand the output. Commented Jul 17, 2017 at 6:24
  • @JosefHoppe from the UB. Commented Jul 17, 2017 at 6:24
  • @AjayBrahmakshatriya as in printf("x=%d, y=%p, z=%p\n", x, y, z); ? Commented Jul 17, 2017 at 6:25

1 Answer 1

2

Let's break it down.

  • int x=30, *y, *z; y,z are defined and x is initialized.
  • y=&x; z=y; both of the pointers are assigned with the address of x (i.e. &x)
  • *y++=*z++; This one contains a few things, so I'll split it up:

  1. According to this the postfix++ takes precedence over indirection* so we can look at *y++ as *(y++)
  2. y++ means that the value of y will be incremented by 1 and since y is int* it will actually add sizeof(int) to it's value [4 in your case]. But the value of the expression y++ is the value of y before the increment, which leads us to...
  3. indirection: after the postfix++ is done, the indirection is evaluated and since y++'s value is the value of "old" y you get the value of x

Conclusion: y was incremented by 1 but the indirection (dereference) led us to x.

The same applies to z. Eventually what happens is that both y,z are incremented, but before that the value in x is assigned to x (obsolete :) )

Next you'll have x++; which increments the value of x by 1. And that's it.

You can think of *y++=*z++; as:

  1. *y = *z;
  2. y++, z++;
Sign up to request clarification or add additional context in comments.

11 Comments

The value 500 is assumed, as this is a practice exercise. /* Assume address of x is 500 and integer is 4 byte size */
@WCKennedays, you mean *++y? then it is UB because the pointer y+1 is not necessarily valid.
@WCKennedays then you mean ++*y = ++*z? that is again UB because you are writing to the same pointer twice between 2 sequence points.
C11 Standard (draft n1570) § 6.5 (2) of Expressions is your reference
@CIsForCookies 'Since postfix++ takes precedence over *' It does not - first value of the *p is taken, then p is incremented so you can : while(*p) putch(*p++);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.