Skip to content
Related Articles
Open in App
Not now

Related Articles

C Quiz – 111 | Question 1

Improve Article
Save Article
  • Last Updated : 28 Jun, 2021
Improve Article
Save Article

Pick the best statement for the following program snippet:

#include <stdio.h>
int main()
 int var;  /*Suppose address of var is 2000 */
 void *ptr = &var;
 *ptr = 5;
 printf("var=%d and *ptr=%d",var,*ptr);
 return 0;

(A) It will print “var=5 and *ptr=2000”
(B) It will print “var=5 and *ptr=5”
(C) It will print “var=5 and *ptr=XYZ” where XYZ is some random address
(D) Compile error

Answer: (D)

Explanation: Key point in the above snippet is dereferencing of void pointer. It should be noted that dereferencing of void pointer isn’t allowed because void is an incomplete data type. The correct way to assign value of 5 would be first to typecast void pointer and then use it. So instead of *ptr, one should use *(int *)ptr. Correct answer is d.

Quiz of this Question

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!