ProgrammingHardTCS NQT · Infosys pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int a = 5;
int *p = &a;
void *vp = p;
int *p2 = (int *)vp;
*p2 = 99;
printf("%d %d", a, *p);- A. 99 99
- B. 5 5
- C. 99 5
- D. 5 99
Correct answer: A. 99 99
A void* can hold any object pointer's value without loss and be cast back to the correct type; p2 is really just another int* to a, so *p2 = 99 writes directly into a. Both a and *p (reading the same a) now show 99.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {1, 2, 3}; int p = a; int q = &a[2]; ptrdifft diff = q - p; printf("%td %d", diff…
- 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 a = 10; int p = &a; p += 5; p = p; p = 2; printf("%d", a);
- Assume a 64-bit LP64 ABI. What is printed? c int a[2][2] = {{1, 2}, {3, 4}}; int p = a[0]; int q = a[1]; printf("%ld", q - p);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 1; int b = 2; int parr[2]; parr[0] = &a; parr[1] = &b; int pp = parr; for (int i = 0…
- What is printed? c void f(int n) { if (n == 0) return; printf("%d ", n); f(n - 1); printf("%d ", n); } int main(void) { f(3); }