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;
}
Answer: D
Correct answer: x=31, y=504, z=504
*y++ = *z++ assigns *z to *y - writing 30 back over itself - and then increments both pointers, since ++ in the postfix position binds to the pointer rather than to the dereferenced value.
Pointer arithmetic is scaled by the pointed-to type, so each advances by sizeof(int), that is 4 bytes: from 500 to 504. x++ then makes x 31. Both pointers now point one past x and must not be dereferenced.
Please log in or register to report this question.