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 6 of 10.
James said:
1 decade ago
This is all about precedence, suffix ++ has higher precedence than * and prefix ++. So, *y++ equal to *(y++). Since ++ is suffix, So first *y and then y++.
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.
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.
Sundeep katta said:
1 decade ago
Since Z and y have same values in them y.
The statment *y++=*z++ actually assigns the value in z (address of x)to ythus Y++ makes it 500, similarly z++.
The statment *y++=*z++ actually assigns the value in z (address of x)to ythus Y++ makes it 500, similarly z++.
Gabor said:
10 years ago
@Bhargav:
The x=30 is saved from 500-503, because the '30' (integer) value is 4 byte size. So the next free address is 504.
The x=30 is saved from 500-503, because the '30' (integer) value is 4 byte size. So the next free address is 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.
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.
Y is pointing to the address of x. But at the end z is pointing to the original location of x i.e, 500.
Sangram said:
1 decade ago
Here x = 30; y = z = 500;
Then,
*z++ = 504 = *y++;//as it takes 4 bytes.
x++ = 31;
So the answer is 31, 504, 504.
Then,
*z++ = 504 = *y++;//as it takes 4 bytes.
x++ = 31;
So the answer is 31, 504, 504.
Sadaf Rahman said:
1 decade ago
In a pointer char stores 2 bytes, int stores 4 bytes, long stores 8 bytes.
Hence in above example x=504 and y=504.
Hence in above example x=504 and y=504.
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!
Means 504 = 504;
Can anyone tell me how can I assign a value to a value?
Is this not an L-value ERROR!
Chidananda said:
5 years ago
Since they mentioned integer is 4 bytes. If we increment from 500 address it will take next 4th address i.e is 504.
(7)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers