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 2 of 10.
Krishan said:
1 decade ago
Any pointer can be type-casted to void pointer and void pointer can be type-casted again to any pointer type to operate on the data.
You can say void pointer contains raw data which has no meaning. Typecasting it gives it a proper meaning provided we already know that type of data which it contains.
You can say void pointer contains raw data which has no meaning. Typecasting it gives it a proper meaning provided we already know that type of data which it contains.
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.
Akshay K said:
1 decade ago
In given code, vptr is a void pointer and 'i' is integer variable.
But vptr assigned the address of integer variable without typecasting.
see code:
---------------
int main()
{
void *vptr;
vptr = &i;
--------------
So, it takes value as 0.
But vptr assigned the address of integer variable without typecasting.
see code:
---------------
int main()
{
void *vptr;
vptr = &i;
--------------
So, it takes value as 0.
Sumit kumar nager said:
1 decade ago
i is a global variable so its value is 0 by default.
vptr = &i; \\we can assign any kind of address to void type variable.
q = (int**)&p; \\ this is type casting void pointer is converted into int
So at the end we have
i=0;
q=0;
vptr = &i; \\we can assign any kind of address to void type variable.
q = (int**)&p; \\ this is type casting void pointer is converted into int
So at the end we have
i=0;
q=0;
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
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.
Sachin said:
1 decade ago
@Seema.
i is declared global its default value is 0 as well as it is stored in bss(block starting with symbol);
One more thing is that if it was initialized then it will be stored on the data section of memory.
i is declared global its default value is 0 as well as it is stored in bss(block starting with symbol);
One more thing is that if it was initialized then it will be stored on the data section of memory.
Raj Naik said:
1 decade ago
@Anu : yeah..void *vptr is valid.
A void pointer is pointer which has no specified data type and the void pointer can be pointed to any type. If needed, the type can be casted as : int **q =(int **)&vptr.
A void pointer is pointer which has no specified data type and the void pointer can be pointed to any type. If needed, the type can be casted as : int **q =(int **)&vptr.
Aadhi said:
1 decade ago
q caries an int value , whereas **q caries address of another pointer.
q=10; //address of q say, 0x44
*p=&q; //value of p=0x44 and its own address say, 0x40
**r=&p; //value of r=0x40
q=10; //address of q say, 0x44
*p=&q; //value of p=0x44 and its own address say, 0x40
**r=&p; //value of r=0x40
Suresh said:
7 years ago
As per my knowledge;
main()
{
void *ptr=&p;
if want printf print ptr required type casting >>*(int*)&p;
simlary in double pointer >>**(int**)&p; this thing happen
}
main()
{
void *ptr=&p;
if want printf print ptr required type casting >>*(int*)&p;
simlary in double pointer >>**(int**)&p; this thing happen
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers