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 4 of 10.
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.
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.
Anu said:
1 decade ago
void *vptr;
Is it correct?
Is it correct?
Rachit said:
1 decade ago
Please tell me the significance of (int**) because if we write (int) or (int****...) then also the answer is same.
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.
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.
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.
Seema said:
1 decade ago
As i is declared global and default value of global variables is 0.
Jerin said:
1 decade ago
in (int**)p p is typecasted to integer pointer.
Sujay said:
1 decade ago
Can any one explain me about (int**) &p;?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers