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

Anusha katta said:   1 decade ago
How is the answer D?.

And can anyone tell the significance of, *y++=*z++; ?.

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

Steffi said:   1 decade ago
In windows the pointer gets incremented by 4-bytes and since in the above stmt y and z are incremented the value is 504.

Ripu n ajit said:   1 decade ago
As x=30; y=z= 500;

*z++=504= *y++;

x++=31;

So the answer is 31, 504, 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

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

Cpverma said:   1 decade ago
y++ n *y++ is not same......
But in case of
*y++=*z++
*(indirection operator) have no effect becoz they are pointing the same value.so it will cancel out...
like and behave like y++=z++

Rk balaji said:   1 decade ago
@cp verma.

You didn't explain the difference?

Pranoy Gn said:   1 decade ago
@eneryone

In ++*y the value the pointer is pointing to will be incremented
Bit, in *y++ the address will be incrementedand it is given as 4 bytes for integer and address is 500 given. So
In *y ==> y=500 ==>*y++ ==> y=504
and x++=31

Basant Sukhdev said:   1 decade ago
It's very easy...

Here y=z; means both has same address i.e 500..ok...
now this expression *y++=*z++; means "whatever the the VALUE AT Z++ ,assign it to the address of y++"


note:-here *y++ & *z++ both are post increment so...firstly expression(*y++=*z++;) is performed.

so...value at z is assigned to y(i.e nothing but 30)

and after dat both address is incremented by 4 bytes(according to the question)

so finally..
x++;
becomes 31 and z,y becomes 504,504


Post your comments here:

Your comments will be displayed after verification.