In Turbo C (DOS), determine the sizes printed by this program mixing dereferences in sizeof:
#include
int main()
{
char huge *near *far *ptr1;
char near *far *huge *ptr2;
char far *huge *near *ptr3;
printf("%d, %d, %d
", sizeof(ptr1), sizeof(**ptr2), sizeof(ptr3));
return 0;
}
-
A4, 4, 4
-
B4, 2, 2
-
C2, 8, 4
-
D2, 4, 8
Answer
Correct Answer: 4, 2, 2
Explanation
Introduction / Context:sizeof can be applied either to a pointer variable or, after dereferencing, to another pointer type. This question mixes multi-level pointers with near/far/huge and asks you to resolve sizes under Turbo C (16-bit DOS).
Given Data / Assumptions:
- near = 2 bytes; far = 4 bytes; huge = 4 bytes.
- ptr1 is a far pointer; ptr2 is a huge pointer to a far pointer to a near char pointer; ptr3 is a near pointer.
Concept / Approach:Unroll types stepwise. Each * removes one indirection level and exposes the next pointer's qualifier, which determines sizeof at that level.
Step-by-Step Solution:1) sizeof(ptr1): ptr1 is far * ⇒ 4.2) sizeof(**ptr2): *ptr2 is a far *; **ptr2 is a near * ⇒ 2.3) sizeof(ptr3): ptr3 is near * ⇒ 2.4) Hence: 4, 2, 2.
Verification / Alternative check:Print sizeof of each intermediate type in a separate test to see 4 (far *) and 2 (near *).
Why Other Options Are Wrong:Option A: Treats all three as 4-byte pointers.Option C/D: Introduce impossible 8-byte sizes or wrong bindings.
Common Pitfalls:Assuming dereferencing always yields a char rather than a pointer type at the next level; here **ptr2 still yields a pointer (near *).
Final Answer:4, 2, 2