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 8 of 10.

Shashank said:   1 decade ago
The value refer to the pointer p& as the q refer to the extern variable, so the value is 0.

Abs said:   1 decade ago
Because i is declared as extern and the default value of an extern variable is 0 ... i think :)

Ishwar lal janwa said:   1 decade ago
How come 0 and the fun call & is not define why ?

Adhiraj said:   1 decade ago
Because i is declared as extern and the default value of an extern variable is 0. Global variable always allocate the memory and initialize to zero. !

Atul said:   1 decade ago
Yes it is correct void pointer is typecast in integer pointer.
So extern variable is always initialized by 0.
Due to this reason show o/p 0.

Suriya said:   1 decade ago
How can I know I is the extern variable or global?

Deepak said:   1 decade ago
(int **) &p. Its type casting of value passed from actual to formal arguments.

As int i is global and value is not assigned so default value is 0.

So 0 is output.

Sachendra said:   1 decade ago
@Anil ,i we don't declare i as a global then it will print garbage value that is stored in i.

Anil said:   1 decade ago
If we don't declare i as a global, then what will be o/p?

Aadhi said:   1 decade ago
q caries an int value , whereas **q caries address of another pointer.
q=10; //address of q say, 0x44
*p=&q; //value of p=0x44 and its own address say, 0x40
**r=&p; //value of r=0x40


Post your comments here:

Your comments will be displayed after verification.