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

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.

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

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

Shahil Sabbag said:   1 decade ago
@Vasuvandan.

(1) (2) when compiler tries to execute y++=z++;

It shows error because:

y++ is written as y=y+1.

Similarly z=z+1; So it looks like y=y+1=z=z+1.

But with *y++=*z++ first of all *y=*z is executed.

*z=30 so *y=30. And after that there is no more assignment meanwhile y++ and z++ are executed separately without any assignment.

Hence compiler run successfully.

Kavitha said:   1 decade ago
#include<stdio.h>
3 int main()
4 {
5 int a[5]={2,3,1,8,6};
6 int *p,*q,i;
7 p=a;
8 q=a;
9 for(i=0;i<5;i++)
10 printf(" p=%d q=%d\n",(*p)++,(*q)++);
11 }

Output:

p=3 q=2.
p=5 q=4.
p=7 q=6.
p=9 q=8.
p=11 q=10.

Vasuvandan said:   1 decade ago
Clarify my particular doubts only?

1) Why compiler says error when I execute y++=z++?

2) Some people said *y++ = *z++ can be done as:

I) *y =*z.
II) y++, z++.

Actually what does mean *y=*z when y=z=500?

3) And also explain right to, left associations?

BSP said:   1 decade ago
*y++=*z++.

The above statement execute as:

Value of z(500) is initialize to y(500), and the value of y(500) is incremented to 504, and then the value of z will get increment i.e. 500 to 504.

Rajesh said:   1 decade ago
x=30;
y=& x;=>*y=30.

They said that x address location is 500th and size is 4 bytes.

*y++=*z++=>*y=31.

x++=31;

y and z will jump to next location i.e. 504.

That is why x=31, y=504, z=504.

Murali said:   1 decade ago
@Basant Sukdev.

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

Saurav pradhan said:   1 decade ago
Since ++ and * have the same precedence and it'll be evaluated from right to left so the answer will be 500 and integer 4 bytes will added to 500 so it will be 504

*y++ = *z++;.

We had assume address of y as 500 and added integer 4 bytes to it.

And evaluated the expression from right to left.

Therefore answer will be 504 for y and z. And x is incremented to 31.

Teju said:   1 decade ago
X=30;
X++=31;
Y=&X;
&X=500;
Y=500;

Z=Y=500; SO *Y++=500+4=504 because pointer size is 4 bytes given.

*z++=*y++; *z++,*y++ is same.

So x=31;z=504;y=504.


Post your comments here:

Your comments will be displayed after verification.