Turbo C (DOS): evaluate sizeof results that mix pointer levels and dereferences:\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 combines multi-level pointers with near/far/huge qualifiers and requires resolving the type at each dereference before applying sizeof in Turbo C (16-bit DOS).


Given Data / Assumptions:

  • near = 2 bytes; far = 4 bytes; huge = 4 bytes.
  • ptr1 is far; ptr2 is far; *ptr3 is far.


Concept / Approach:
Unwrap one level at a time and record the qualifier at that level. sizeof reports the size of the pointer representation at the evaluated type.


Step-by-Step Solution:
1) sizeof(ptr1): ptr1 is far * ⇒ 4.2) sizeof(*ptr2): ptr2 is huge * to (far *) ⇒ *ptr2 is far * ⇒ 4.3) sizeof(**ptr3): ptr3 is near * to (huge * to (far *)) ⇒ **ptr3 is far * ⇒ 4.4) Therefore the output is 4, 4, 4.


Verification / Alternative check:
Print intermediate sizeof values for *ptr2 and **ptr3 to confirm both are far pointers.


Why Other Options Are Wrong:
Options B/C/D: Misidentify which level is being measured by sizeof or assume an incorrect 2-byte result.


Common Pitfalls:
Jumping directly to the base char type instead of recognizing that dereferencing here still yields a pointer type.


Final Answer:
4, 4, 4

More Questions from Complicated Declarations

Discussion & Comments

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