What will be the output of the program ?
#include <stdio.h>
void fun(void *p);
int i;
int main()
{
void *vptr;
vptr = &i;
fun(vptr);
return 0;
}
void fun(void *p)
{
int **q;
q = (int**)&p;
printf("%d\n", **q);
}
Answer: D
Correct answer: 0
i is a global, so it has static storage duration and is zero-initialised before the program starts.
Inside fun, &p is the address of the parameter, which itself holds &i. Casting that to int ** and applying ** therefore reads first the parameter, obtaining &i, and then i itself - giving 0. The double cast is a roundabout way of reaching a value that *(int*)p would have given directly.
Please log in or register to report this question.