Pointer size question across platforms: Given
#include
int main()
{
float p;
printf("%d
", sizeof(p));
return 0;
}
What is sizeof(float) typically on 16-bit versus 32-bit compilers?
-
A2 on a 16-bit compiler, 4 on a 32-bit compiler
-
B4 on a 16-bit compiler, 2 on a 32-bit compiler
-
C4 on both 16-bit and 32-bit compilers
-
D2 on both 16-bit and 32-bit compilers
-
EAlways 8 regardless of platform
Answer
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