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);- A. 42
- B. Compile-time error: void* cannot be assigned from int*
- C. Undefined behaviour: void* cannot be safely cast back
- 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
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {1, 2, 3}; int p = &a[1]; printf("%d %d", p[-1], p[1]);
- Assume a 64-bit LP64 ABI. What is printed? c int x = 5; int p = &x; int pp = &p; pp = NULL; printf("%d", p == NULL);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 10; int p = &a; int q = p; p = NULL; printf("%d %d", p == NULL, q);
- Assume a 64-bit LP64 ABI. What is printed? c int rows = 2, cols = 3; int matrix = malloc(rows sizeof(int )); for (int i = 0; i < rows…
- Assume a 64-bit LP64 ABI. What is printed? c int x = 1; int p = &x; int pp = &p; int ppp = &pp; printf("%d", ppp == &pp && ppp == pp &&…
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {10, 20, 30}; int b[3] = {10, 20, 30}; printf("%d %d", a == b, a == b);