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 6 of 10.
Paddu said:
1 decade ago
Answer is 0 because i is external variable.
Default value of external variable is 0.
Default value of external variable is 0.
Annapurna said:
1 decade ago
Can we declare void as a datatype for parameters and how the compiler treats it?
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;
Sunil said:
1 decade ago
i is declare as global variable & default value of global variable is 0.
Ankitradhe said:
1 decade ago
Global variable default value is 0.
Sujay said:
1 decade ago
Can any one explain me about (int**) &p;?
Jerin said:
1 decade ago
in (int**)p p is typecasted to integer pointer.
Seema said:
1 decade ago
As i is declared global and default value of global variables is 0.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers