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 9 of 10.
Manik said:
8 years ago
Here the answer is 0, because 'i' is declared as extern (all the global variables have storage class 'extern'), and all extern variables have an initial value equal to zero.
Darshan said:
8 years ago
Thank you all for the given explanation.
Chintan said:
8 years ago
Thank you @Shivam.
Ravi said:
8 years ago
Thank you for all.
Saranya.s said:
8 years ago
I am not getting this, anyone please explain clearly.
(1)
Alekhya said:
8 years ago
How can you assign a value to null pointer?
(1)
Kowsik said:
8 years ago
What will be output of program if 'i' is initialised globally as int i=5?
Maheswari said:
8 years ago
If we initialized i=5 it will print 5. Whatever I value it will print that value.
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
}
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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers