Turbo C (DOS): mix of near/far/huge with sizeof at different dereference depths — what is printed?\n\n#include<stdio.h>\n\nint main()\n{\n char huge *near *far *ptr1;\n char near *far huge ptr2;\n char far huge near ptr3;\n printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));\n return 0;\n}

Difficulty: Medium

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

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion