On a 16-bit DOS/Turbo C platform where pointers are 2 bytes in small/near memory models, consider the following node structure allocation. What are the sizes printed for the pointer variables p and q? #include<stdio.h> #include<stdlib.h> int main() { struct node { int data; struct node *link; }; struct node *p, *q; p = (struct node *) malloc(sizeof(struct node)); q = (struct node *) malloc(sizeof(struct node)); printf("%d, %d\n", sizeof(p), sizeof(q)); return 0; }

Difficulty: Easy

Correct Answer: 2, 2

Explanation:


Introduction / Context:
This item checks understanding that sizeof(pointer) depends on the platform and memory model, not on the pointed-to type. In classic 16-bit DOS small model, near pointers are 2 bytes.


Given Data / Assumptions:

  • Turbo C / DOS 16-bit near-pointer model.
  • sizeof(p) and sizeof(q) are measured; both are pointers to struct node.
  • malloc returns a pointer; the allocation size does not change sizeof(pointer).


Concept / Approach:
sizeof(p) yields the size of the pointer variable p itself. On 16-bit near model, this is 2 bytes regardless of p’s target type. Same applies to q.


Step-by-Step Solution:

sizeof(p) = 2 bytes (near pointer) sizeof(q) = 2 bytes (near pointer) Printed result -> "2, 2"


Verification / Alternative check:
Check compiler documentation for Turbo C’s small model: near data/code pointers are 2 bytes; far are 4. Without far qualifiers here, near size applies.


Why Other Options Are Wrong:

  • 8, 8 and 4, 4: These reflect 32-bit or 64-bit modern platforms, not 16-bit DOS small model.
  • 5, 5: No standard pointer size of 5 bytes in this context.


Common Pitfalls:
Confusing sizeof(*p) (size of struct node) with sizeof(p) (size of pointer). Also assuming modern 4- or 8-byte pointers universally.


Final Answer:
2, 2

More Questions from Structures, Unions, Enums

Discussion & Comments

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