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

Zohra said:   1 decade ago
Well explained. Shivam. Thanks.

Piyush said:   1 decade ago
Hi guys,

Its answer is 0, because I is declare as global. So its by default zero.

Krishan said:   1 decade ago
Any pointer can be type-casted to void pointer and void pointer can be type-casted again to any pointer type to operate on the data.

You can say void pointer contains raw data which has no meaning. Typecasting it gives it a proper meaning provided we already know that type of data which it contains.

Kapil said:   1 decade ago
Shivam nice explanation.

Gireesh said:   1 decade ago
Can anyone tell about void. Actually what is mean by void?

Triven Sharma said:   1 decade ago
Shivam is absolutely right

Venky498 said:   1 decade ago
Nice explanation shivam. As you said here void pointer is generic pointer.

So, we can assign address of any data type to it (e.g: int,float,..), so it is correct way to write like this vptr=&i;

and "int i" is global variable,so its default valu is 0.

Sapna said:   1 decade ago
By default global assign value 0. So answer is 0.

You can check by assigning value to i, That value will display.

Eg. If i=4, then output 4.

Dpol.. said:   1 decade ago
Is I extern variable as extern is not mentioned. It should be automatic according to me? please elaborate.

Raghu said:   1 decade ago
q = (int**) &p ---> typecasting is not necessary, since void can be assigned to int or anytype. However compiler will through an "WARNING".

Since q is declared to be a pointer to a pointer to and integer, by casting the void pointer to int we are helping the compiler to treat the variable as int.


Post your comments here:

Your comments will be displayed after verification.