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.

Shivam said:   1 decade ago
Let me explain u all.....u all are in doubt....

vptr=&i;

this line is alright. because vptr is void pointer..and this is the property of void pointer that it can point to any type of variable(eq. int, float or char etc)....

int i;\\this is declared outside main so it is of extern type. so it contains default val 0.

Now whenever we have to asign a value pointed by a void pointer to int we have to type cast it.

q = (int**) &p;.here q is a double pointer of int type.
p is a void pointer.so to make q point the address of void pointer q , we have to type cast it into int** ..

thats all............
so **q will be 0= value of i

Santosh Batta said:   1 decade ago
Hi everybody
int main():
the vptr is void pointer and i is int type. So, first it will show error as per the statement written in problem
vptr = &i;

Next thing is: q = (int**) &p;
in this case q is a memory location (i.e pointer) which stores the memory location of another variable that holds an integer value. OR simply we can say it is a double pointer of integer type. AND p is a void pointer. So, void pointer can not point to any datatype unless and untill it is type-casted to that particular datatype. So, the statement q=(int**)&p is valid.

Apurva Nigam said:   1 decade ago
Thanks to evrybdy....

So it means we could have written
q = &p;
if both were 'int' pointers.

Here p is void thats y we need to typecast....??

And 1 more Question:-

Can we typecast 'int' pointer to 'void'?
I feel no.

Santhosh said:   1 decade ago
I Can't understand the program. Can anyone explain?

Sanjay kr. said:   1 decade ago
I can't understand can any one explain please.

Ch.suresh said:   1 decade ago
I think void *p represent a null pointer so , null pointer means it pointing to null=0.
eg: *p->0(null)

Kalyan said:   1 decade ago
As p is the void pointer so we have to convert to an integer pointer so we made the conversion. And extern varible default value is 0.

Satya RKV said:   1 decade ago
@Mangesh is right.

For preethi answer is &p is nothing but we will get the address of p pointer and we are typecasting to as int multiple indirection.

Mangesh said:   1 decade ago
Because global veriable is set to 0 (by defult).

Preethi said:   1 decade ago
What does this line mean actually?.

q = (int**) &p;.

Can anyone explain me please?.


Post your comments here:

Your comments will be displayed after verification.