Machine-translated by Google. Questions are authored in English.
Top

Discussion

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);
}

 

  • A.Error: cannot convert from void** to int**
  • B.Garbage value
  • C.No output
  • D.0

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.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.