Discussion
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);
}
Answer: D
Correct answer: 0
i is a global, so it has static storage duration and is zero-initialised before the program starts.
Inside fun, &p is the address of the parameter, which itself holds &i. Casting that to int ** and applying ** therefore reads first the parameter, obtaining &i, and then i itself - giving 0. The double cast is a roundabout way of reaching a value that *(int*)p would have given directly.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development