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);
}
Error: cannot convert from void** to int**
Garbage value
0
No output
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
95 comments Page 5 of 10.

Aadhi said:   1 decade ago
q caries an int value , whereas **q caries address of another pointer.
q=10; //address of q say, 0x44
*p=&q; //value of p=0x44 and its own address say, 0x40
**r=&p; //value of r=0x40

Anil said:   1 decade ago
If we don't declare i as a global, then what will be o/p?

Sachendra said:   1 decade ago
@Anil ,i we don't declare i as a global then it will print garbage value that is stored in i.

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.

Suriya said:   1 decade ago
How can I know I is the extern variable or global?

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.

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. !

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.

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............

Udaysiri said:   1 decade ago
void is null when void is given in local variable it's print 0.


Post your comments here:

Your comments will be displayed after verification.