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 4 of 10.

Maheswari said:   8 years ago
If we initialized i=5 it will print 5. Whatever I value it will print that value.

Kowsik said:   8 years ago
What will be output of program if 'i' is initialised globally as int i=5?

Ravi said:   8 years ago
Thank you for all.

Chintan said:   8 years ago
Thank you @Shivam.

Darshan said:   8 years ago
Thank you all for the given explanation.

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.

Divya said:   8 years ago
Local variable will initialize garbage values, as well as global variables will initialize as 0.

Nagma said:   9 years ago
Because 'i' is a global variable not extern.

Vicky said:   1 decade ago
Since it has the type void it is called as null pointer. Which is general purpose pointer. Instead of declaring pointer as int, float, char as 3 times.

We can use null pointer. Which stores address of any datatype for eg: int, float.

Because it does not have any type, compiler does not know what datatype pointer is holding. For that we have to do typecast.

int main()
{
int inum = 8;
float fnum = 67.7;
void *ptr;
ptr = &inum;
printf("\nThe value of inum = %d",*((int*)ptr));//tells compiler that ptr is holding integer value and returns 8
ptr = &fnum;
printf("\nThe value of fnum = %f",*((float*)ptr));//tells compiler that ptr is holding float value and returns 67.7
return(0);
}

PRASHANT SISODIYA said:   10 years ago
Any one tell me why void not convert to int A option is not the answer?


Post your comments here:

Your comments will be displayed after verification.