+ 1

Why is it showing 4 in output?

int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} }; myArr[0][2] = 42; int x = myArr[1][0]; // 4

2nd Mar 2018, 2:00 PM
F_uck
3 odpowiedzi
+ 7
You declared a two dimension array with 3 rows and (partially) three columns; Column 1 2 3 ... n Row 1 {1,2,3} Row 2 {4} Row 3 {5,6,7} ... Row n Because we use zero based index to refer to array elements, myArr[1] [0] refer to the first column of the second row. P.S. Exactly as @Danilo explained : )
2nd Mar 2018, 2:19 PM
Ipang
+ 4
In most programming languages, the first element of an array has index '0'; the second element has index '1' and so on. myArr[0][2] refers to the third element ([2]) of the first array ([0]). So basically, you first need to know which array you are talking about, and then you go after the element of that array, i.e., someArray[array][element_of_array].
2nd Mar 2018, 2:12 PM
Danilo
Danilo - avatar
+ 2
got it! thanks
2nd Mar 2018, 2:14 PM
F_uck