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 10 of 10.
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.
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.
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.
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers