ProgrammingHardTCS NQT · Infosys pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
struct S { int a; int b; };
struct S arr[3] = {{1,2},{3,4},{5,6}};
int *p = (int *)arr;
printf("%d %d", p[2], p[5]);- A. 3 6
- B. 5 6
- C. 3 4
- D. 1 6
Correct answer: A. 3 6
Since struct S has no padding (both members are int, naturally aligned), arr is laid out as a flat contiguous sequence of 6 ints: {1,2,3,4,5,6}. p[2] = 3 (arr[1].a), p[5] = 6 (arr[2].b).
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c struct A { int x; }; struct B { struct A a; }; struct B b1 = {{10}}; struct B b2 = b1; b2.a.x…
- Assume a 64-bit LP64 ABI. What is printed? c struct S { double d; char c; }; struct S arr[2]; printf("%zu %ld", sizeof(arr), (char…
- Assume a 64-bit LP64 ABI. What is printed? c union Shape { struct { double radius; } circle; struct { double width, height; } rect; }…
- Assume 32-bit int and two's complement. What is printed? c int x = 5; int y = ~x + 1; printf("%d", y);
- Assume 32-bit int. What is printed? c int x = 12; printf("%d %d %d", x & 1, x | 1, x ^ 1);
- Assume 32-bit int. What is printed? c int a = 6, b = 3; printf("%d %d %d %d", a & b, a | b, a ^ b, ~a);