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.

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

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

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

Venky said:   1 decade ago
@Geetha void function () means the function returns nothing not 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.

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.

PRASHANT SISODIYA said:   10 years ago
Any one tell me why void not convert to int A option is not the answer?

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.

Nagma said:   9 years ago
Because 'i' is a global variable not extern.

Divya said:   8 years ago
Local variable will initialize garbage values, as well as global variables will initialize as 0.


Post your comments here:

Your comments will be displayed after verification.