Double indirection and 2D array decay: What value prints?\n#include<stdio.h>\nvoid fun(int **p);\n\nint main()\n{\n int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};\n int *ptr;\n ptr = &a[0][0];\n fun(&ptr);\n return 0;\n}\nvoid fun(int **p)\n{\n printf("%d\n", *p);\n}

Difficulty: Easy

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

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion