Which one of the choices given below would be printed when the following…
2006
Which one of the choices given below would be printed when the following program is executed?
#include<stdio.h>
int a1[] = {6, 7, 8, 18, 34, 67};
int a2[] = {23, 56, 28, 29};
int a3[] = {-12, 27, -31};
int *x[] = {a1, a2, a3};
void print(int *a[])
{
printf("%d,", a[0][2]);
printf("%d,", *a[2]);
printf("%d,", *++a[0]);
printf("%d,", *(++a)[0]);
printf("%d\n", a[-1][+1]);
}
main()
{
print(x);
}
Answer: A. 8, -12, 7, 23, 8 — Concept: When a function parameter is declared as (equivalently ), the indexing rule exp[i] means *(exp + i) at every level: a[i] dereferences the i-th…
- A.
8, -12, 7, 23, 8
- B.
8, 8, 7, 23, 7
- C.
-12, -12, 27, -31, 23
- D.
-12, -12, 27, -31, 56
Attempted by 103 students.
Show answer & explanation
Correct answer: A
Concept: When a function parameter is declared as (equivalently ), the indexing rule exp[i] means *(exp + i) at every level: a[i] dereferences the i-th pointer stored in the array, and a[i][j] means *(*(a+i)+j) — the j-th element of the row that the i-th pointer refers to. A prefix increment applied to an INDEXED pointer object, such as ++a[0], increments the pointer VALUE stored at that memory slot — a side effect that persists in the underlying array of row-pointers. A prefix increment applied to the pointer VARIABLE itself, such as ++a, only rebinds that local variable to the next slot without touching the array it pointed into.
Application:
Initial layout: a1 = {6, 7, 8, 18, 34, 67}, a2 = {23, 56, 28, 29}, a3 = {-12, 27, -31}; the row-pointer array holds a1 at slot 0, a2 at slot 1, a3 at slot 2, and the parameter a initially refers to slot 0.
a[0][2]: a[0] is the row-pointer at slot 0 (a1); *(a1+2) = a1[2] = 8.
*a[2]: a[2] is the row-pointer at slot 2 (a3); *(a3+0) = a3[0] = -12.
*++a[0]: ++a[0] increments the pointer VALUE stored at slot 0 (from pointing at a1[0] to pointing at a1[1]) — this change is written back into the row-pointer array; dereferencing then gives a1[1] = 7.
*(++a)[0]: ++a increments the local pointer VARIABLE a, so a now refers to slot 1 instead of slot 0; (++a)[0] reads the row-pointer stored at slot 1 (a2, unchanged); dereferencing gives a2[0] = 23.
a[-1][+1]: since a now refers to slot 1, a[-1] refers back to slot 0 — whose stored pointer was permanently advanced in the earlier step to point at a1[1]; a[-1][+1] therefore evaluates to *(a1+1+1) = a1[2] = 8.
Cross-check: tracking the row-pointer array and the local variable a independently confirms the same trace:
Stage | slot 0 points to | slot 1 points to | slot 2 points to | a refers to |
|---|---|---|---|---|
Before any step | a1[0] | a2[0] | a3[0] | slot 0 |
After *++a[0] | a1[1] | a2[0] | a3[0] | slot 0 |
After *(++a)[0] | a1[1] | a2[0] | a3[0] | slot 1 |
Reading the last row of the table: a is at slot 1, so a[-1] is slot 0, which by then stores a1[1]; indexing +1 from there gives a1[2] = 8 — matching the step-by-step trace above.
Final output: 8, -12, 7, 23, 8
Explore the full course: Iocl Engineers Officers Grade A Paper 2