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

Monika said:   1 decade ago
I got the answer, but after (*y++=*z++;) this expression what will the why & z point to? and what is the value at the address 504?

Please provide me full explanation.

Gautam said:   1 decade ago
@Vikash.

Because '=' has right to left associativity means left operand should be unambiguous but in case of y++ = z++ the '=' can't find an unambiguous operand(++ or y++).

Vikash said:   1 decade ago
Someone please tell.
Why compiler doesn't execute

y++ = z++;

while execute *y++ = *z++;

Sekar said:   1 decade ago
1. The address of x which is assumed as 500 and size is 4 byte passed to pointer y.

2. The address stored in y is passed to pointer z.

3. Then both y and z get post increment that is 500 to 504. because the size is 4 byte.

4. the value of x again incremented to 30 to 31;

ANS is = x = 31, y = 504, and z = 504.

Brijkishor said:   1 decade ago
First x = 30.

x++ means x= 31.

*y++ means *(y++) =*(z++) it means y = z = 504.

Means only pointer of address variable i.e y and z increase not value.

Vaibhav said:   1 decade ago
*y++ = *z++;
Means 504 = 504;
Can anyone tell me how can I assign a value to a value?
Is this not an L-value ERROR!

Srk said:   1 decade ago
Consider value of x is stored in address 500.

So y = z = 500.

If we increment the value of the pointer it will be incremented to point next location where a new integer will be stored.

*z++=500+sizeof (int) *1 == 504.

Same thing happens for y.

Garima said:   1 decade ago
x++ means increment of 1 in x value. So x=31.

Same as *y++ and *z++ but difference is integer x contain 4 byte size so the value of *y++ and *z++ is 504 because x address is 500.

Amol said:   1 decade ago
*y++=*z++ can broken down as
*y=*z;
y++;
z++;
Because it is post increment operator.

Maggie said:   1 decade ago
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=30;//variable x has 30;
y=&x//y has 500;
z=y;//rigth to left ie y has 500 and its assigned to z so z=500
//y and z both pointing to x
*y++=*z++//here as per post increment rules 1st we should assign and then increment so here *z has 30, assign it to y and it sits in *y so IN THIS LINE both y and z has 30
x++//x increments

And while printing
x =31;
And y which is having 500 will be incremented to 504 because in this line *y++ means y++ is 500 to 504 and then *
In this line the value would have got incremented to 504 na
and so is z.


Post your comments here:

Your comments will be displayed after verification.