ProgrammingVery hardProduct company OA
What is printed?
What is printed?
/* LP64: 4-byte int, 8-byte pointers */
void f(int m[][4], int n) {
printf("%zu %zu %td ", sizeof m, sizeof m[0], (char *)(m + n) - (char *)m);
}
int main(void) {
int a[3][4] = {0};
f(a, 2);
printf("%zu", sizeof a);
}- A. 48 16 32 48
- B. 8 16 2 48
- C. 8 16 32 48
- D. 8 4 32 48
Correct answer: C. 8 16 32 48
A parameter declared int m[][4] is adjusted to int (*m)[4], a pointer to a row, so sizeof m is 8. m[0] is a whole row of 4 ints: 16 bytes. m + n advances by n rows, i.e. 2 × 16 = 32 bytes (a plain 2 would be the difference in rows, not bytes). In main, a is a real array: 3 × 4 × 4 = 48.
More C Programming questions
- What is printed? c int a = 1, b = 2, c = 3; a += b -= c = 2; int d = (a, b) > c ? a-- : c++; printf("%d %d %d %d", a, b, c, d);
- What is printed? c int a[] = {1, 2, 3, 4, 5}; int p = a + 1, q = &a[4]; printf("%td %d %d", q - p, p + q, p[2]);
- With 4-byte int, what is printed? c int a[3][4] = {0}; int (p)[4] = a; int q = &a[0][0]; printf("%td %td %td", (p + 2) - p, (char )(p + 1)…
- What is printed? c int calls = 0; int f(int n) { calls++; return n < 2 ? n : f(n / 2) + f(n - 1); } int main(void) { int r = f(6)…
- Assume 32-bit unsigned int. What is printed? c unsigned x = 0xB8; / 1011 1000 / printf("%u %u %u", x & (x - 1), x | (x - 1), x & -x);
- What is printed? c int f(void) { static int n = 5; return n--; } int main(void) { int s = 0; while (f() > 2) s++; printf("%d %d", s, f()); }