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

Kalyan kedarnath said:   5 years ago
q=(int**)&p // Typecasting.

i initial value is Zero 0 because Global variable.
(1)

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.
(2)

Chidananda said:   5 years ago
Default value of global variable is 0.
(2)

Shishir Singh said:   5 years ago
Default value of global variable is set to 0 (zero).
(6)

Mayur22kar said:   4 years ago
#include<stdio.h>

void fun(void *p);
int i; // global variable

int main()
{
void *vptr; // void poniter is created hear
vptr = &i; // pointer vptr store the address of variable i
fun(vptr); // call to the function fun with passing pointer variable vptr
return 0;
}
void fun(void *p) // function fun with one pointer variable p to store the address of vptr
{
int **q; // hear is create pointer of pointer to store the address of another pointer variable
q = (int**)&p; // hear p is an void pointer it type cast to the integer pointer q is and integer pointer hear
printf("%d\n", **q); // hear print value store in pointer q.
}
(6)


Post your comments here:

Your comments will be displayed after verification.