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 1 of 10.
Mayur22kar said:
4 years ago
#include<stdio.h>
void fun(void *p);
int i; // global variable
int main()
{
void *vptr; // void poniter is created hear
vptr = &i; // pointer vptr store the address of variable i
fun(vptr); // call to the function fun with passing pointer variable vptr
return 0;
}
void fun(void *p) // function fun with one pointer variable p to store the address of vptr
{
int **q; // hear is create pointer of pointer to store the address of another pointer variable
q = (int**)&p; // hear p is an void pointer it type cast to the integer pointer q is and integer pointer hear
printf("%d\n", **q); // hear print value store in pointer q.
}
void fun(void *p);
int i; // global variable
int main()
{
void *vptr; // void poniter is created hear
vptr = &i; // pointer vptr store the address of variable i
fun(vptr); // call to the function fun with passing pointer variable vptr
return 0;
}
void fun(void *p) // function fun with one pointer variable p to store the address of vptr
{
int **q; // hear is create pointer of pointer to store the address of another pointer variable
q = (int**)&p; // hear p is an void pointer it type cast to the integer pointer q is and integer pointer hear
printf("%d\n", **q); // hear print value store in pointer q.
}
(6)
Vicky said:
1 decade ago
Since it has the type void it is called as null pointer. Which is general purpose pointer. Instead of declaring pointer as int, float, char as 3 times.
We can use null pointer. Which stores address of any datatype for eg: int, float.
Because it does not have any type, compiler does not know what datatype pointer is holding. For that we have to do typecast.
int main()
{
int inum = 8;
float fnum = 67.7;
void *ptr;
ptr = &inum;
printf("\nThe value of inum = %d",*((int*)ptr));//tells compiler that ptr is holding integer value and returns 8
ptr = &fnum;
printf("\nThe value of fnum = %f",*((float*)ptr));//tells compiler that ptr is holding float value and returns 67.7
return(0);
}
We can use null pointer. Which stores address of any datatype for eg: int, float.
Because it does not have any type, compiler does not know what datatype pointer is holding. For that we have to do typecast.
int main()
{
int inum = 8;
float fnum = 67.7;
void *ptr;
ptr = &inum;
printf("\nThe value of inum = %d",*((int*)ptr));//tells compiler that ptr is holding integer value and returns 8
ptr = &fnum;
printf("\nThe value of fnum = %f",*((float*)ptr));//tells compiler that ptr is holding float value and returns 67.7
return(0);
}
Swapnil said:
1 decade ago
i is neither static nor extern. It's a variable visible for the compilation unit it's in, and additionally will be visible from all compilation units that declare x to be an extern variable.
Why am I saying it's neither static nor extern?
If it was extern, then, there must be a different compilation unit with x declaration on it. Clearly this is your only compilation unit.
If it was static then, no extern reference would be allowed to x variable defined in this compilation unit. We know that we could easily declare an extern variable to this x declared here.
Why is 0 assigned to x? Because, in C, all global variables initialize to 0. It says so in 6.7.8 (10) of the C99 standard.
Why am I saying it's neither static nor extern?
If it was extern, then, there must be a different compilation unit with x declaration on it. Clearly this is your only compilation unit.
If it was static then, no extern reference would be allowed to x variable defined in this compilation unit. We know that we could easily declare an extern variable to this x declared here.
Why is 0 assigned to x? Because, in C, all global variables initialize to 0. It says so in 6.7.8 (10) of the C99 standard.
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
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.
JacksPP said:
1 decade ago
Actually 4 basic concepts are :
NULL is not Zero or any value.
NULL simply refers to nothing.
void *vptr is NULL pointer is right.
vptr = &i; --> points to address of i.
In void fun(void *p) --> accept NULL pointer as a parameter.
int **q; --> it is double pointer which points to another pointer ie. q pointer.
It means address of p is in pointer q.
q = (int**)&p; --> External typecasting is done.
void pointer to integer pointer.
Value of external variable i is 0.
.
.
.
Thus answer is 0............
NULL is not Zero or any value.
NULL simply refers to nothing.
void *vptr is NULL pointer is right.
vptr = &i; --> points to address of i.
In void fun(void *p) --> accept NULL pointer as a parameter.
int **q; --> it is double pointer which points to another pointer ie. q pointer.
It means address of p is in pointer q.
q = (int**)&p; --> External typecasting is done.
void pointer to integer pointer.
Value of external variable i is 0.
.
.
.
Thus answer is 0............
Shiv said:
6 years ago
int i ; // declared globally & Global variables are automatically initialized to 0 at the time of declaration.
fun(vptr); // pass vptr as addrrss in fun function.
fun(void *p)
{ }; //take vptr in *p pointer
q=(int**)&p. // q is a pointer of pointer which store address of *p pointer
( int**) used for typecasting.
**q=*p=i
So simply printf print the value of i and we know that Global variables are automatically initialized to 0 at the time of declaration.
fun(vptr); // pass vptr as addrrss in fun function.
fun(void *p)
{ }; //take vptr in *p pointer
q=(int**)&p. // q is a pointer of pointer which store address of *p pointer
( int**) used for typecasting.
**q=*p=i
So simply printf print the value of i and we know that Global variables are automatically initialized to 0 at the time of declaration.
(3)
Shambhu said:
1 decade ago
In this program i is declared as a global variable. And we know that global variable initially initialized to be 0.
So int i=0;
Now void pointer(it is a pointer which points any data type)pointing to the base address of int i.
So it point the base address of i(means 0).
1st step:
vptr contain the address of i, so it contain 0.
and passes it through function.
2nd step:
In fun....
We assign the value contain in *p in q.
So dereference it we get the value present in i.
i.e----> 0.
So int i=0;
Now void pointer(it is a pointer which points any data type)pointing to the base address of int i.
So it point the base address of i(means 0).
1st step:
vptr contain the address of i, so it contain 0.
and passes it through function.
2nd step:
In fun....
We assign the value contain in *p in q.
So dereference it we get the value present in i.
i.e----> 0.
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....
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