ProgrammingHardIBM · Deloitte pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
int x = 42;
void *vp = &x;
int *ip = (int *)vp;
printf("%d", *ip);
  1. A. 42
  2. B. Compile-time error: void* cannot be assigned from int*
  3. C. Undefined behaviour: void* cannot be safely cast back
  4. D. 0

Correct answer: A. 42

void* is a generic pointer type that any object pointer converts to (and from, with an explicit cast) without loss of information; casting vp back to int* and dereferencing it correctly recovers the original int, 42. This round-trip through void* is completely well-defined as long as the cast-back type matches the original pointee type.

More C Programming questions

Exams that ask this topic