ProgrammingVery hardProduct company OA
Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed?
Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed?
int a[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int (*p)[4] = a + 1;
int *q = *p + 2;
printf("%d %d %d %td %d %zu", (*p)[1], q[1], q[-2], q - a[1], p[1][0], sizeof *p / sizeof **a);- A. 6 8 5 2 9 4
- B. 6 8 5 2 9 2
- C. 2 4 1 2 5 4
- D. 6 8 5 6 9 4
Correct answer: A. 6 8 5 2 9 4
a + 1 has type int (*)[4] and points at the whole second row, so (*p)[1] = 6 and p[1] is the third row (p[1][0] = 9). *p decays to &a[1][0], hence q = &a[1][2]: q[1] = 8, q[-2] = 5 and q − a[1] = 2 elements. sizeof *p is a full row, 16 bytes, and **a is one int, so the ratio is 4. The option ending in 2 confuses sizeof *p with the size of a pointer (8).
More C Programming questions
- Assume 32-bit int and two's complement. What is printed? c unsigned char a = 0x0F; unsigned char b = ~a; printf("%d %d %d %d %zu", ~a ==…
- Assume 32-bit int and 64-bit long long. What is printed? c int n = -1; unsigned int len = 3; long long big = -1; printf("%d %d %d %d", n <…
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed? c struct A { char c; double d; int…
- What is printed? c #define SQ(x) x x #define MIN(a, b) ((a) < (b) ? (a) : (b)) int calls = 0; int next(void) { return ++calls; } int…
- What is printed? c #define N 4 #define STR(x) #x #define XSTR(x) STR(x) #define CAT(a, b) a##b #define XCAT(a, b) CAT(a, b) int NN = 9…
- Which of the numbered statements violate a constraint (i.e. must be rejected by the compiler)? c typedef char strt; #define STRM char char…