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 9 of 10.
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
So getting all these thing output would be --> 0
Thanx
Adarsh said:
1 decade ago
Thanks to all
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.
q = (int**)&p;
Here void ptr is casted into integer ptr.
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.
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.
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)
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers