Double indirection and 2D array decay: What value prints?
#include
void fun(int **p);
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
int *ptr;
ptr = &a[0][0];
fun(&ptr);
return 0;
}
void fun(int **p)
{
printf("%d
", *p);
}
-
A1
-
B2
-
C3
-
D4
-
ECompilation error due to mismatched types
Answer
Correct Answer: 1
Explanation
Introduction / Context:This item tests decay of a 2D array to a pointer to its first element and correct use of double indirection when passing a pointer by reference to a function.
Given Data / Assumptions:
- a is a 3x4 int matrix initialized row-major.
- ptr is set to &a[0][0], the address of the first element.
- fun receives &ptr, i.e., a pointer to an int (type int *).
Concept / Approach:Inside fun, parameter p points to ptr; dereferencing once yields the original int; dereferencing twice yields the int value stored at that location. Since ptr points to the first element of a, **p is a[0][0] which equals 1.
Step-by-Step Solution:ptr = &a[0][0] → ptr holds address of the first integer.fun(&ptr) passes the address of ptr.In fun, *p == ptr and *p == ptr == a[0][0] == 1.
Verification / Alternative check:Change initialization so a[0][0] has a distinct value to confirm the print reflects that element; or print p, p to inspect addresses.
Why Other Options Are Wrong:They assume access to other elements; the code never offsets ptr before printing.
Common Pitfalls:Confusing type int ()[4] (pointer to array) with int; here we explicitly take &a[0][0] to get an int.
Final Answer:1