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);
}
Discussion:
95 comments Page 8 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
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
Sravanthi said:
1 decade ago
I didn't understand where variable i is declared as extern can any one tell me?
SREE 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)
eg: *p->0(null)
HEMANTH said:
1 decade ago
THANKS PRIYA...........
Abhishek shrivastava said:
1 decade ago
Because I is declared as extern, and external variable always will have 0;.
Aneesha said:
1 decade ago
Uninitialized static and extern(global) varibles are by default set to 0.
Tekme2god said:
1 decade ago
I am not clear all about this thing. Please help me.
Sivakumarn said:
1 decade ago
I don't know how integer pointer convert to null pointer any body please can you explain.
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....
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....
Pankaj said:
1 decade ago
What a question any one have a answer with exmpl. ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers