C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 6)
6.
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);
}
Error: cannot convert from void** to int**
Garbage value
0
No output
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
95 comments Page 3 of 10.

Sujay said:   1 decade ago
Can any one explain me about (int**) &p;?

Ankitradhe said:   1 decade ago
Global variable default value is 0.

Sunil said:   1 decade ago
i is declare as global variable & default value of global variable is 0.

Sumit kumar nager said:   1 decade ago
i is a global variable so its value is 0 by default.

vptr = &i; \\we can assign any kind of address to void type variable.

q = (int**)&p; \\ this is type casting void pointer is converted into int

So at the end we have
i=0;
q=0;

Annapurna said:   1 decade ago
Can we declare void as a datatype for parameters and how the compiler treats it?

Paddu said:   1 decade ago
Answer is 0 because i is external variable.

Default value of external variable is 0.

Udaysiri said:   1 decade ago
void is null when void is given in local variable it's print 0.

Harsha n said:   1 decade ago
The global variable of int type is initialized to 0 in c and hence the result.

The type casting can be done in C, but to pass the int value to the void there is no need of casting.

Manish said:   10 years ago
As 'i' is a global variable whose value is not initialized, so its default value is set to 0.

The pointer q is accessing i it prints 0.

Suresh said:   7 years ago
As per my knowledge;

main()
{
void *ptr=&p;
if want printf print ptr required type casting >>*(int*)&p;
simlary in double pointer >>**(int**)&p; this thing happen
}


Post your comments here:

Your comments will be displayed after verification.