Pick the best statement for the above C program snippet.
(A) Assigning (void *)0 to pVoid isn’t correct because memory hasn’t been allocated. That’s why no compile error but it’ll result in run time error.
(B) Assigning (void *)0 to pVoid isn’t correct because a hard coded value (here zero i.e. 0) can’t assigned to any pointer. That’s why it’ll result in compile error.
(C) No compile issue and no run time issue. And the size of the void pointer i.e. pVoid would equal to size of int.
(D) sizeof() operator isn’t defined for a pointer of void type.
Explanation: (void *)0 is basically NULL pointer which is used for many purposes in C. Please note that no matter what is the type of pointer, each pointer holds some address and the size of every pointer is equal to sizeof(int). So D) isn’t correct. An absolute address can be assigned to any pointer though it might result in issues at run time if the address is illegal. Since 0 is a legal address, assigning (void *)0 to pVoid is fine. So B) isn’t correct. We aren’t doing any illegal operation with pVoid here. So it’ll not result in any compile/run time error. So A) isn’t correct. For example, if we perform illegal operation over pVoid e.g. de-referencing of void pointer i.e. *pVoid, it’ll result in error. The above program will compile/run without any issue. So C) is correct.
Quiz of this Question
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.