Under Turbo C (16-bit DOS), evaluate the sizes printed by this program with mixed near/far/huge on multi-level pointers:
#include
int main()
{
char huge *near *far *ptr1; // ptr1: far pointer to (near pointer to huge char)
char near *far *huge *ptr2; // ptr2: huge pointer to (far pointer to near char)
char far *huge *near *ptr3; // ptr3: near pointer to (huge pointer to far char)
printf("%d, %d, %d
", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));
return 0;
}
-
A4, 4, 8
-
B2, 4, 4
-
C4, 4, 2
-
D2, 4, 8
Answer
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