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 5 of 10.
Ankitradhe said:
1 decade ago
Global variable default value is 0.
Sunil said:
1 decade ago
i is declare as global variable & default value of global variable is 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;
Annapurna said:
1 decade ago
Can we declare void as a datatype for parameters and how the compiler treats it?
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.
Udaysiri said:
1 decade ago
void is null when void is given in local variable it's print 0.
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............
Harsha n said:
1 decade ago
The global variable of int type is initialized to 0 in c and hence the result.
The type casting can be done in C, but to pass the int value to the void there is no need of casting.
The type casting can be done in C, but to pass the int value to the void there is no need of casting.
Adhiraj said:
1 decade ago
Because i is declared as extern and the default value of an extern variable is 0. Global variable always allocate the memory and initialize to zero. !
Atul said:
1 decade ago
Yes it is correct void pointer is typecast in integer pointer.
So extern variable is always initialized by 0.
Due to this reason show o/p 0.
So extern variable is always initialized by 0.
Due to this reason show o/p 0.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers