2D array in C with pointer -
I am trying to understand code output below:
int counter = 0 ; Intetrix [5] [5]; Register int * aPtr; Int i, j; For (i = 0; i & lt; 5; i ++) (j = 0; j & lt; 5; j ++) aMatrix [i] [j] = counter ++; APTR = A and amatrix [1] [1]; Printf ("% d \ n", APTR [2]);
Referring to the sample code above, what would be the value of "aPtr [2]" after execution?
Please help me understand why I am getting the output as 8.
for (i = 0; i <5; i ++) For (J = 0; j & lt; 5; j ++) Amatrix [i] [j] = Counter ++;
After the execution of the loop, your aMatrix
-
amatrix [0] - & gt; 0 1 2 3 4 AMTRICS [1] - & gt; 5 6 7 8 9 AMTrix [2] - & gt; 10 11 12 13 14 AMTrix [3] - & gt; 15 16 17 18 19 AMTrix [4] - & gt; 20 21 22 23 24
then aMatrix [1] [1]
contains 6
, you specify the address of There are amatrix [1] [1]
to aPtr
. That is aPtr [0] = 6
, aPtr [1] = 7
and aPtr [2] = 8
. So obviously you will get the output 8
Comments
Post a Comment