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 2 of 10.
Neeraj said:
1 decade ago
just try the following program and u will get the difference between *ptr++ and ++*ptr
# include<stdio.h>
int main()
{
int arr[]={15,6,9,22,8,21};
int i,*ptr;
ptr=arr;
for(i=0;i<6;i++)
printf("%d\t",*ptr++);
return 0;
}
Now replace printf() statement with:
printf("%d\t",++*ptr);
In *ptr++,the pointer is incrememted whereas in ++*ptr,the vaue is incremented.
# include<stdio.h>
int main()
{
int arr[]={15,6,9,22,8,21};
int i,*ptr;
ptr=arr;
for(i=0;i<6;i++)
printf("%d\t",*ptr++);
return 0;
}
Now replace printf() statement with:
printf("%d\t",++*ptr);
In *ptr++,the pointer is incrememted whereas in ++*ptr,the vaue is incremented.
MAK said:
1 decade ago
Initially x=30, y=500 , z=500
*y++=*z++;
'y'& 'z' is address of x. But after executing above step the value in address of 'y' , 'z' is incremented to 31, but it cannot store in same location (Since 30 is stored in address 500), so it will store it next location 504.
x++;
x is incremented to 31,
printf("x=%d, y=%d, z=%d\n", x, y, z);
Now x is 31, y & z is 504
*y++=*z++;
'y'& 'z' is address of x. But after executing above step the value in address of 'y' , 'z' is incremented to 31, but it cannot store in same location (Since 30 is stored in address 500), so it will store it next location 504.
x++;
x is incremented to 31,
printf("x=%d, y=%d, z=%d\n", x, y, z);
Now x is 31, y & z is 504
Shubham said:
6 years ago
#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; // initialization z by 500
*y++=*z++; // pointer size is 4 byte when we use the increment operater at that time pointer incremented by the pointer size.
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y; // initialization z by 500
*y++=*z++; // pointer size is 4 byte when we use the increment operater at that time pointer incremented by the pointer size.
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
(4)
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.
(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.
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.
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.
*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.
Naveena said:
9 years ago
I understood the answer but I have some confusion why not the answer is 31,31,31 because if we remove the line *y++=*z++; it is printing *y=30,*z=30, then if we increase *y++ it has to be 31 as *y is 30 and also for *z but why it incrementing address, why not value but while printing *y why it is not printing 500 but it is 30?
Pease give me the clear explanation.
Pease give me the clear explanation.
Tejaswi Gunti said:
10 years ago
In case of *y++ and *z++ the precedence is from right to left. So first the address of y and z is incremented and then the value is assigned because of the indirection (*).
int size is 4 bytes so address becomes address +4. So the answer is D.
i.e, x=31 (since x++).
y = 504 (If we take the base address as 500).
z = 504 (If we take base address as 500).
int size is 4 bytes so address becomes address +4. So the answer is D.
i.e, x=31 (since x++).
y = 504 (If we take the base address as 500).
z = 504 (If we take base address as 500).
(1)
Sahibpreet Singh said:
2 years ago
*(value of address) and increment/decrement operators lie in the same level of precedence. Their precedence level proceeds in right to left manner.
So, if ++ is to the left of * then * will be evaluated first.
Similarly, (in this case) ++ is to the right of * so y++ is evaluated first. *y++ is same as *(y++) and ++*y is same as ++(*y).
So, if ++ is to the left of * then * will be evaluated first.
Similarly, (in this case) ++ is to the right of * so y++ is evaluated first. *y++ is same as *(y++) and ++*y is same as ++(*y).
(8)
Aditya Sekhar said:
1 decade ago
It is all dependent on the associativity of unary operators
! ~ ++ -- + - * (type) sizeof Associativity -- right to left
therefore *y++ = *(y++); (From right side ++ comes first )
same *x++ = * (x++) ; (From right to left )
so (y++) == *(504) is assigned (x++) == *(504) value
so it wont reflect in the printf statement
! ~ ++ -- + - * (type) sizeof Associativity -- right to left
therefore *y++ = *(y++); (From right side ++ comes first )
same *x++ = * (x++) ; (From right to left )
so (y++) == *(504) is assigned (x++) == *(504) value
so it wont reflect in the printf statement
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers