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

Nandkishor said:   10 years ago
int i; is declare as external. Hence for external storage class default value is 0. That is the reason why it prints 0.

Ashish said:   1 decade ago
That's because I is declared as a global and and not been initialized. Default value of global variable is 0.

Venky said:   1 decade ago
@Geetha void function () means the function returns nothing not 0.

Viki said:   1 decade ago
Does void pointer returns zero?

Mahi khan said:   1 decade ago
How 0 is output and what is the use of int**?

Geetha said:   1 decade ago
void methods always returns zero I think:).

Prasun said:   1 decade ago
By default the global variable has default value 0. So answer is 0.

Mohamed mohsen said:   1 decade ago
Because i is global variable by default compiler initialize it to zero.

Manoj & Vinay said:   1 decade ago
Actually void pointer always needs to be casted whenevr we perform any operation on it.

q = (int**)&p;

Here void ptr is casted into integer ptr.

Sravanthi said:   1 decade ago
I didn't understand where variable i is declared as extern can any one tell me?


Post your comments here:

Your comments will be displayed after verification.