C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 3)
3.
What will be the output of the program ?
#include<stdio.h>

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;
}
x=31, y=502, z=502
x=31, y=500, z=500
x=31, y=498, z=498
x=31, y=504, z=504
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
92 comments Page 8 of 10.

Bhargav said:   10 years ago
*y++=*z++;

I got whole concept related to this. But why it is incremented by 4 instead of 1?

Master_86 said:   10 years ago
How z changed?

Y is pointing to the address of x. But at the end z is pointing to the original location of x i.e, 500.

Gabor said:   10 years ago
@Bhargav:

The x=30 is saved from 500-503, because the '30' (integer) value is 4 byte size. So the next free address is 504.

Pari said:   10 years ago
Can anybody clear my doubt regarding post increment and pre increment in C? Cause here, post increment is taking place hence first value should be returned the it have to be increment. But here incremented value is returning?

Tejaswi Gunti said:   10 years ago
In case of *y++ and *z++ the precedence is from right to left. So first the address of y and z is incremented and then the value is assigned because of the indirection (*).

int size is 4 bytes so address becomes address +4. So the answer is D.

i.e, x=31 (since x++).

y = 504 (If we take the base address as 500).

z = 504 (If we take base address as 500).
(1)

Pallvi said:   10 years ago
Why post incr returning incremented value?

Siri said:   9 years ago
How to write 500? Please tell me.

Deep said:   9 years ago
See in *y++ first y++ is performed and gives 500+4 504 then it performs *(y++) ie *(504) so y value changes to 504.

Shobhit Kumar said:   9 years ago
On compiling the answer came as x = 31, y = 2293104, z = 2293104.

Anushka said:   9 years ago
This question has some error, as the pointer *y++ and *z++ will point to the value x=30, and 500 is value of y and z respectively not the value of pointer *y and *z, if the statement would be like y++ = z++, then the answer would be option D.


Post your comments here:

Your comments will be displayed after verification.