In C, analyze multi-dimensional array initialization and deep dereferencing. What does this print?
#include
int main()
{
int arr[3][3] = {1, 2, 3, 4};
printf("%d
", (((arr))));
return 0;
}
-
AOutput: Garbage value
-
BOutput: 1
-
COutput: 3
-
DError: Invalid indirection
-
EOutput: 4
Answer
Correct Answer: Error: Invalid indirection
Explanation
Introduction / Context:This problem tests understanding of how 2-D arrays are initialized and how dereferencing operators apply to array types and pointers. The expression uses three dereferences with parentheses that matter a lot.
Given Data / Assumptions:
- Declaration: int arr[3][3] = {1, 2, 3, 4}; which initializes arr[0] = {1,2,3}, arr[1] = {4,0,0}, arr[2] = {0,0,0}.
- Print statement: (((arr))).
- Standard C array-to-pointer decay rules apply in expressions.
Concept / Approach:arr has type “array of 3 arrays of 3 int”. In expressions, arr usually decays to a pointer to its first element, i.e., int ()[3]. A single * applied to that pointer yields the first row, which is an array of 3 int. When used in expressions, that array further decays to int pointing to arr[0][0]. A second * then yields the int at arr[0][0] (which is 1). However, the third * attempts to dereference that int as if it were a pointer, which is illegal.
Step-by-Step Solution:arr → decays to pointer to row: int ()[3].(arr) → the first row (type: array[3] of int), which in expression context decays to int*.((arr)) → the first int (arr[0][0]) as an lvalue int.((*(arr))) → applying * to an int → invalid indirection → compile-time error.
Verification / Alternative check:Remove the last *: printf("%d", ((arr))); still incorrect type usage. Correct forms include printf("%d", **arr); which prints 1, because **arr equals arr[0][0].
Why Other Options Are Wrong:They assume successful compilation and execution. Here the compiler will emit an invalid indirection error before any run occurs.
Common Pitfalls:Forgetting that an int cannot be dereferenced; miscounting levels of indirection with array decay.
Final Answer:Error: Invalid indirection