Turbo C (16-bit, DOS): sizes of near, far, and huge pointers when printed via sizeof on pointer variables #include<stdio.h> int main() { char *s1; // near pointer (default model) char far *s2; // far pointer char huge *s3; // huge pointer (normalized, same size as far) printf("%d, %d, %d ", sizeof(s1), sizeof(s2), sizeof(s3)); return 0; }

Difficulty: Medium

Correct Answer: 2, 4, 4

Explanation:


Introduction / Context:
In 16-bit DOS memory models, pointer types can differ in size depending on near/far/huge qualifiers. Turbo C commonly uses 16-bit near pointers by default and 32-bit far/huge pointers. Understanding this is vital for legacy code and memory model portability.



Given Data / Assumptions:

  • Platform: Turbo C (16-bit).
  • char *s1; is a near pointer (16-bit, offset only).
  • char far *s2; is a far pointer (segment:offset, 32-bit).
  • char huge *s3; is also 32-bit in size but uses normalized arithmetic.


Concept / Approach:
sizeof(pointer) yields the storage size of the pointer value, not the memory block it points to. Under 16-bit compilers: near = 2 bytes, far = 4 bytes, huge = 4 bytes. Hence the output “2, 4, 4”.



Step-by-Step Solution:
Evaluate sizeof(s1) → 2.Evaluate sizeof(s2) → 4.Evaluate sizeof(s3) → 4.Print “2, 4, 4”.



Verification / Alternative check:
Switching memory models in project options can change the default pointer size, but explicitly qualified far/huge remain 4 bytes; near remains 2 bytes.



Why Other Options Are Wrong:
A/E suggest 6-byte pointers, which Turbo C does not use. B/D misstate sizes for the qualifiers.



Common Pitfalls:
Confusing pointer size with addressable memory; assuming modern 32/64-bit behavior applies to 16-bit environments.



Final Answer:
2, 4, 4

More Questions from Declarations and Initializations

Discussion & Comments

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