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

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)

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

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

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)

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

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

Shiv said:   5 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.
(3)

Suresh said:   6 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
}

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

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

Alekhya said:   7 years ago
How can you assign a value to null pointer?
(1)


Post your comments here:

Your comments will be displayed after verification.