Pointer size question across platforms: Given\n#include<stdio.h>\nint main()\n{\n float p;\n printf("%d\n", sizeof(p));\n return 0;\n}\nWhat is sizeof(float) typically on 16-bit versus 32-bit compilers?

Difficulty: Easy

Correct Answer: 2 on a 16-bit compiler, 4 on a 32-bit compiler

Explanation:


Introduction / Context:
This question probes the relationship between pointer size and platform/ABI, not the pointee type. The sizeof operator reports the storage size of the pointer itself.



Given Data / Assumptions:

  • Legacy 16-bit “small/medium memory model” compilers commonly use 16-bit near pointers for code/data, hence 2-byte pointers.
  • Typical 32-bit ABIs use 32-bit addresses, hence 4-byte pointers.
  • We are querying sizeof(p) where p is a float*.


Concept / Approach:
In C, pointer size depends on the target architecture and memory model, not on the pointed-to type. Therefore, sizeof(float*) equals sizeof(void*) on a given platform. Classic DOS 16-bit compilers generally produce 2-byte near pointers, while 32-bit systems use 4-byte pointers. Modern 64-bit ABIs usually use 8-byte pointers (not listed among the choices).



Step-by-Step Solution:
Declare float *p; compute sizeof(p).On 16-bit model → 2 bytes; on 32-bit model → 4 bytes.Note: This holds regardless of float vs int pointer.



Verification / Alternative check:
Print sizeof on target systems; observe that all object pointers share the same size within an ABI.



Why Other Options Are Wrong:
The pointee type does not affect pointer size, making options that swap sizes incorrect. “Always 8” generalizes from 64-bit only and ignores older ABIs.



Common Pitfalls:
Assuming pointer size equals sizeof(pointee); forgetting that %d for sizeof should be cast to int or printed with %zu for size_t.



Final Answer:
2 on a 16-bit compiler, 4 on a 32-bit compiler

More Questions from Floating Point Issues

Discussion & Comments

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