In Turbo C (16-bit DOS memory model), what does this program print for the sizes of the declared pointer variables?\n\n#include<stdio.h>\n\nint main()\n{\n char huge *near *ptr1; // ptr1: near pointer to (huge char *)\n char huge *far *ptr2; // ptr2: far pointer to (huge char *)\n char huge *huge *ptr3; // ptr3: huge pointer to (huge char *)\n printf("%d, %d, %d\n", sizeof(ptr1), sizeof(ptr2), sizeof(ptr3));\n return 0;\n}

Difficulty: Medium

Correct Answer: 2, 4, 4

Explanation:


Introduction / Context:
This question tests how Turbo C (16-bit DOS memory model) represents pointer sizes when using near, far, and huge qualifiers. Understanding declaration binding and how sizeof applies to the pointer variable (not the pointee) is essential for legacy codebases and interviews.


Given Data / Assumptions:

  • Compiler: Turbo C in 16-bit DOS.
  • Pointer sizes: near = 2 bytes, far = 4 bytes, huge = 4 bytes.
  • Declarations: ptr1 is a near pointer; ptr2 is a far pointer; ptr3 is a huge pointer.


Concept / Approach:
In these declarations, the qualifier nearest to the identifier (ptr1/ptr2/ptr3) determines the size of that pointer variable. The qualifiers attached to inner pointer levels affect the types they point to, not the storage size of the outer pointer variable itself.


Step-by-Step Solution:
1) char huge *near *ptr1; ⇒ ptr1 is a near pointer ⇒ sizeof(ptr1) = 2.2) char huge *far ptr2; ⇒ ptr2 is a far pointer ⇒ sizeof(ptr2) = 4.3) char huge huge ptr3; ⇒ ptr3 is a huge pointer ⇒ sizeof(ptr3) = 4.4) Therefore the output is 2, 4, 4.


Verification / Alternative check:
Compile a minimal program and compare sizeof of a variable declared as near, far, and huge in Turbo C; results match the stated sizes.


Why Other Options Are Wrong:
Option A: Assumes 8-byte huge pointer; in Turbo C, huge is 4 bytes.
Option C/D: Misread the binding between qualifiers and the identifier.


Common Pitfalls:
Confusing the size of the pointer variable with the size of the pointed-to type; the latter does not affect sizeof(pointer).


Final Answer:
2, 4, 4

More Questions from Complicated Declarations

Discussion & Comments

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