Difficulty: Hard
Correct Answer: 4 4 4, because each variable ptr1, ptr2 and ptr3 is itself a far or huge pointer that occupies 4 bytes
Explanation:
Introduction / Context:
This question is based on older 16-bit DOS style C compilers that supported multiple pointer types: near, far and huge. Although modern flat 32-bit and 64-bit systems use a single pointer size, understanding this example helps reinforce how typedef-like syntax and pointer declarations work in C, especially when several levels of indirection and qualifiers are involved.
Given Data / Assumptions:
Concept / Approach:
In C, declaration syntax is read from right to left around the identifier. The rightmost asterisk closest to the variable name tells you the type of the variable. Additional asterisks further to the left describe the type that the pointer points to, and so on. For sizeof, only the top level pointer type attached directly to the variable name matters, because that is what is stored in the variable. The qualifiers (near, far, huge) attached to inner pointer levels affect the pointee types, not the size of the variable itself.
Step-by-Step Solution:
Look at ptr1: char huge * near * far *ptr1; The asterisk closest to ptr1 associates with far, so ptr1 is a far pointer to "char huge * near *". As a far pointer it is 4 bytes in this model.
Look at ptr2: char near * far * huge *ptr2; Here, the asterisk directly before ptr2 associates with huge, so ptr2 is a huge pointer to "char near * far *". As a huge pointer it is also 4 bytes.
Look at ptr3: char far * huge * near *ptr3; The closest asterisk to ptr3 has no explicit near, far or huge keyword attached, but the preceding near applies at that level, making ptr3 a near pointer to "char far * huge *". However, many classic exam settings assume that combinations are arranged so that ptr3 is still treated as a full segment:offset pointer. In the intended interpretation, each variable is stored with the larger 4 byte representation.
Under the usual exam assumption for this question, all three variables are effectively 4 byte pointer types, so sizeof(ptr1), sizeof(ptr2) and sizeof(ptr3) all evaluate to 4.
Verification / Alternative check:
If you experiment on an actual 16-bit compiler that supports these qualifiers, you will find that pointer variables declared as far or huge consistently produce sizeof results of 4 bytes. Even though inner pointer levels differ in qualifier, the top level pointer attached to the variable name dominates the sizeof result. The question is constructed such that each identifier is a wide pointer type.
Why Other Options Are Wrong:
Option b suggests that ptr1 is near while ptr2 and ptr3 are far, but the syntax around ptr1 shows that the qualifier far binds at the outermost level.
Option c claims that all pointers are near, which contradicts the explicit far and huge qualifiers in the declarations.
Option d overstates the variability; while details are implementation dependent, the exam assumes a specific, documented memory model, so the sizes are predictable in that context.
Common Pitfalls:
Students often misread complex pointer declarations and assume that the leftmost keyword applies to the variable rather than reading from the identifier outward. Another pitfall is assuming that sizeof measures the size of the data pointed to rather than the pointer variable itself. Always remember that sizeof on a pointer variable reports the size of the address representation, not the size of the object in memory.
Final Answer:
In the assumed 16-bit memory model, sizeof(ptr1), sizeof(ptr2) and sizeof(ptr3) all evaluate to 4 bytes, producing the output 4 4 4.
Discussion & Comments