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);
}
Discussion:
95 comments Page 3 of 10.
Ashish said:
1 decade ago
That's because I is declared as a global and and not been initialized. Default value of global variable is 0.
Venky said:
1 decade ago
@Geetha void function () means the function returns nothing not 0.
Viki said:
1 decade ago
Does void pointer returns zero?
Mahi khan said:
1 decade ago
How 0 is output and what is the use of int**?
Geetha said:
1 decade ago
void methods always returns zero I think:).
Prasun said:
1 decade ago
By default the global variable has default value 0. So answer is 0.
Mohamed mohsen said:
1 decade ago
Because i is global variable by default compiler initialize it to zero.
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);
}
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);
}
Shruthi said:
1 decade ago
Whether *(int*) is same as (int**)?
Indira said:
1 decade ago
Can anyone tell me?
q = (int**)&p = q = (int)&p.
What is the use of (int**).
q = (int**)&p = q = (int)&p.
What is the use of (int**).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers