Turbo C (DOS): mix of near/far/huge with sizeof at different dereference depths — what is printed?
#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
-
B2, 2, 2
-
C2, 8, 4
-
D2, 4, 8
Answer
Correct Answer: 4, 4, 4
Explanation
Introduction / Context:This problem explores how dereferencing exposes the next pointer level's qualifier, which then determines sizeof for that expression in Turbo C. It is about careful parsing, not arithmetic.
Given Data / Assumptions:
- near = 2 bytes; far = 4 bytes; huge = 4 bytes.
- ptr1 is far to (near to (huge to char)).
- ptr2 is huge; ptr3 is near to (huge to (far to char)).
Concept / Approach:Each dereference moves one level inward: ptr1 reaches a huge; ptr2 is itself huge; ptr3 is huge. sizeof of any huge under Turbo C is 4.
Step-by-Step Solution:1) **ptr1 ⇒ huge * ⇒ sizeof(**ptr1) = 4.2) ptr2 ⇒ huge * ⇒ sizeof(ptr2) = 4.3) *ptr3 ⇒ huge * ⇒ sizeof(*ptr3) = 4.4) Output: 4, 4, 4.
Verification / Alternative check:Break the types with typedefs for readability and print sizeof each alias.
Why Other Options Are Wrong:Option B: Treats all as near; Option C/D: Inject impossible 8-byte sizes or wrong bindings.
Common Pitfalls:Forgetting that dereferencing does not necessarily reach the base char; at each step you may still be at a pointer type with its own qualifier.
Final Answer:4, 4, 4