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

Swapnil said:   1 decade ago
i is neither static nor extern. It's a variable visible for the compilation unit it's in, and additionally will be visible from all compilation units that declare x to be an extern variable.

Why am I saying it's neither static nor extern?

If it was extern, then, there must be a different compilation unit with x declaration on it. Clearly this is your only compilation unit.

If it was static then, no extern reference would be allowed to x variable defined in this compilation unit. We know that we could easily declare an extern variable to this x declared here.

Why is 0 assigned to x? Because, in C, all global variables initialize to 0. It says so in 6.7.8 (10) of the C99 standard.

Shambhu said:   1 decade ago
In this program i is declared as a global variable. And we know that global variable initially initialized to be 0.

So int i=0;

Now void pointer(it is a pointer which points any data type)pointing to the base address of int i.

So it point the base address of i(means 0).

1st step:

vptr contain the address of i, so it contain 0.
and passes it through function.

2nd step:

In fun....
We assign the value contain in *p in q.
So dereference it we get the value present in i.
i.e----> 0.

Rachit said:   1 decade ago
Please tell me the significance of (int**) because if we write (int) or (int****...) then also the answer is same.

Anu said:   1 decade ago
void *vptr;

Is it correct?

Raj Naik said:   1 decade ago
@Anu : yeah..void *vptr is valid.

A void pointer is pointer which has no specified data type and the void pointer can be pointed to any type. If needed, the type can be casted as : int **q =(int **)&vptr.

Akshay K said:   1 decade ago
In given code, vptr is a void pointer and 'i' is integer variable.
But vptr assigned the address of integer variable without typecasting.

see code:
---------------
int main()
{
void *vptr;
vptr = &i;
--------------

So, it takes value as 0.

Indira said:   1 decade ago
Can anyone tell me?

q = (int**)&p = q = (int)&p.

What is the use of (int**).

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

Seema said:   1 decade ago
As i is declared global and default value of global variables is 0.

Jerin said:   1 decade ago
in (int**)p p is typecasted to integer pointer.


Post your comments here:

Your comments will be displayed after verification.