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

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.

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.

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.

Adarsh said:   1 decade ago
Thanks to all

Mohd kamarshad said:   1 decade ago
Because vptr is voind pointer type it have compatability to convert to any type pointer and i is extern variable which have global access and it's default values is 0

So getting all these thing output would be --> 0

Thanx

Pankaj said:   1 decade ago
What a question any one have a answer with exmpl. ?

Priya said:   1 decade ago
I'll explain d pgm...
p is a void pointer,void pointer implies tat it can point to any type...
q=(int**)&p---->here void pointer(ie.p) is converted to int pointer before assigning it to q,which is an int pointer.

vptr=&i---->here int pointer is assigned to void pointer(ie. vptr),since it can pt any type.

finally q gets value as 0 since i is an extern var,default value is 0.
tats all....

Sivakumarn said:   1 decade ago
I don't know how integer pointer convert to null pointer any body please can you explain.

Tekme2god said:   1 decade ago
I am not clear all about this thing. Please help me.

Aneesha said:   1 decade ago
Uninitialized static and extern(global) varibles are by default set to 0.


Post your comments here:

Your comments will be displayed after verification.