Under Turbo C (16-bit DOS), evaluate the sizes printed by this program with mixed near/far/huge on multi-level pointers:\n\n#include<stdio.h>\n\nint main()\n{\n char huge *near *far *ptr1; // ptr1: far pointer to (near pointer to huge char)\n char near *far *huge *ptr2; // ptr2: huge pointer to (far pointer to near char)\n char far *huge *near *ptr3; // ptr3: near pointer to (huge pointer to far char)\n printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));\n return 0;\n}

Difficulty: Medium

Correct Answer: 4, 4, 2

Explanation:


Introduction / Context:
This problem checks how pointer qualifiers near/far/huge bind to the identifier and how sizeof reports the size of each variable in Turbo C (16-bit DOS).


Given Data / Assumptions:

  • Pointer sizes: near = 2 bytes, far = 4 bytes, huge = 4 bytes.
  • sizeof reports the size of the pointer variable's representation.
  • All code compiled in 16-bit small/compact memory models where these keywords are meaningful.


Concept / Approach:
Read declarations right-to-left. The qualifier directly adjacent to the identifier (ptr1/ptr2/ptr3) governs the representation size of that pointer variable.


Step-by-Step Solution:
1) ptr1 is declared as far * ⇒ sizeof(ptr1) = 4.2) ptr2 is declared as huge * ⇒ sizeof(ptr2) = 4.3) ptr3 is declared as near * ⇒ sizeof(ptr3) = 2.4) Output sequence: 4, 4, 2.


Verification / Alternative check:
Create single-variable tests with the same qualifiers and inspect sizeof to confirm.


Why Other Options Are Wrong:
Option A/D: Assume 8-byte pointers or misassign sizes.
Option B: Incorrectly treats ptr1 or ptr2 as near pointers.


Common Pitfalls:
Attaching the qualifier to the wrong level of indirection, or believing inner pointer qualifiers influence the outer pointer's sizeof.


Final Answer:
4, 4, 2

More Questions from Complicated Declarations

Discussion & Comments

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