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 7 of 10.

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.

Sudarshan said:   1 decade ago
Here x = 30; y = z = 500;
Then,

*z++ = 504 = *y++;//inc takes 4 bytes.
x++ = 31;

So the answer is 31, 504, 504.

Murali said:   1 decade ago
@Basant Sukdev.

Good explanation clear view. Really I understood on first on first step. Others also done well.

Xyz said:   1 decade ago
Since given integer 4 bytes and above statement y and z are incrementing, y and z will take 504.

Sandeep said:   1 decade ago
1).
y=&x so y=500;
z=y so y=z=500;
so *y++=*z++=504
and x++=31

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

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

Verma said:   1 decade ago
y=&x so y=500;
z=y so y=z=500;
so *y++=*z++=504
and x++=31
so ans is: x=31, y=504, z=504

Nibash pandey said:   1 decade ago
y=&x so y=500;
z=y so y=z=500;
so *y++=*z++=504
and x++=31
so ans is: x=31, y=504, z=504

Xstream said:   1 decade ago
In all the operating system pointers will be incremented by 4 bytes. so the answer is D(504)

Abc said:   1 decade ago
Can anyone tell me whats the difference betw y++ and *y++? will both leads the same answer?


Post your comments here:

Your comments will be displayed after verification.