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 3 of 10.
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.
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.
Deepak said:
1 decade ago
(int **) &p. Its type casting of value passed from actual to formal arguments.
As int i is global and value is not assigned so default value is 0.
So 0 is output.
As int i is global and value is not assigned so default value is 0.
So 0 is output.
Satya RKV said:
1 decade ago
@Mangesh is right.
For preethi answer is &p is nothing but we will get the address of p pointer and we are typecasting to as int multiple indirection.
For preethi answer is &p is nothing but we will get the address of p pointer and we are typecasting to as int multiple indirection.
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. !
Manoj & Vinay said:
1 decade ago
Actually void pointer always needs to be casted whenevr we perform any operation on it.
q = (int**)&p;
Here void ptr is casted into integer ptr.
q = (int**)&p;
Here void ptr is casted into integer ptr.
Sapna said:
1 decade ago
By default global assign value 0. So answer is 0.
You can check by assigning value to i, That value will display.
Eg. If i=4, then output 4.
You can check by assigning value to i, That value will display.
Eg. If i=4, then output 4.
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.
Shivani Mundra said:
5 years ago
@All.
i is a global variable and it is uninitialized so it would be stored in a .bss segment so by default the value of i would be zero.
i is a global variable and it is uninitialized so it would be stored in a .bss segment so by default the value of i would be zero.
(2)
Manish said:
10 years ago
As 'i' is a global variable whose value is not initialized, so its default value is set to 0.
The pointer q is accessing i it prints 0.
The pointer q is accessing i it prints 0.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers