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;
}
Discussion:
92 comments Page 9 of 10.
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??
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??
Kishore Mylavarapu said:
1 decade ago
y=&x means y stores address of x which is 500 and *y value is 30.
Now z=y which means z stores the value of y.The value of y is 500.
so z==500.
Now *y++=*z++ which implies *y=30 and *z=500(Since, * operator gives you the value of that pointer).
So *y=500 and *z=500.
Now we have to increment(++).Since, the size is 4 bytes..*y=504 and *z=504.
Now x++ which implies x=31.
Now z=y which means z stores the value of y.The value of y is 500.
so z==500.
Now *y++=*z++ which implies *y=30 and *z=500(Since, * operator gives you the value of that pointer).
So *y=500 and *z=500.
Now we have to increment(++).Since, the size is 4 bytes..*y=504 and *z=504.
Now x++ which implies 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
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
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
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
Rk balaji said:
1 decade ago
@cp verma.
You didn't explain the difference?
You didn't explain the difference?
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++
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++
Abc said:
1 decade ago
Can anyone tell me whats the difference betw y++ and *y++? will both leads the same answer?
Sandeep said:
1 decade ago
1).
y=&x so y=500;
z=y so y=z=500;
so *y++=*z++=504
and x++=31
y=&x so y=500;
z=y so y=z=500;
so *y++=*z++=504
and x++=31
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.
*z++=504= *y++;
x++=31;
So the answer is 31, 504, 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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers