ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int a[2][3] = {{1,2,3},{4,5,6}};
int *rowptrs[2] = {a[0], a[1]};
int **pp = rowptrs;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
pp[i][j] *= 2;
printf("%d %d %d %d", a[0][0], a[0][2], a[1][0], a[1][2]);- A. 4 6 8 12
- B. 1 3 4 6
- C. 2 4 6 8
- D. 2 6 8 12
Correct answer: D. 2 6 8 12
rowptrs[0] and rowptrs[1] alias a's actual rows (not copies), so doubling through pp doubles a in place: a[0][0]=2, a[0][2]=6, a[1][0]=8, a[1][2]=12.
More C Programming questions
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers, natural alignment). What is printed? c struct Inner { char a; short b; }; struct…
- Assume unsigned int bitfields pack into 32-bit allocation units (typical gcc/Linux x86-64 behavior). What is printed? c struct BF {…
- Assume unsigned int bitfields pack into 32-bit allocation units and a zero-width bitfield forces the next field to a new unit. What is…
- On a little-endian 64-bit machine, what is printed? c union U { int i; char c[4]; }; union U u; u.i = 0x41424344; printf("%c%c%c%c"…
- Assume a 64-bit LP64 ABI. What is printed? c struct Variant { int type; union { int i; float f; double d; }; }; printf("%zu"…
- Assume a 64-bit LP64 ABI. What is printed? c struct Node { char tag; struct Node next; int val; }; printf("%zu", sizeof(struct Node));