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.

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.

Kidsid said:   1 decade ago
Hey guys the correct solution is that
here it uses the concept of precedence of operators like
*y=*z
*y++;
*z++;
in case of *z++
the ++ has higher precedence than *
so that it increment z by 4 than get that value instead u should try
(*y)++;
check it out??

Manu said:   1 decade ago
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y;
*y++=(*z)++;
x++;
printf("x=%d,x add=%u, y=%u, z=%u\n", x,&x, y, z);
/run dis code u.ll get to kw d diff b/w (*Z)++ n *(Z++)

Lekha said:   1 decade ago
Its very simple.
X=30 /*assume base address is 500*/
z=y=&x;
z=y=500;
pointer increment to this address will be increment.
*y++=*z++=504;
int will be 4byte.
X will be incremented.
X=31.
Prints that values x=31,y=504,z=504.


Post your comments here:

Your comments will be displayed after verification.