Open In App

C Quiz – 111 | Question 1

Like Article
Like
Save
Share
Report

Pick the best statement for the following program snippet: 
 

C




#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
Please comment below if you find anything wrong in the above post


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads