On Friday, 26 January 2024 at 11:38:39 UTC, Stephen Tashiro wrote:
On Thursday, 25 January 2024 at 20:36:49 UTC, Kagamin wrote:
On Thursday, 25 January 2024 at 20:11:05 UTC, Stephen Tashiro
wrote:
void main()
{
ulong [3][2] static_array = [ [0,1,2],[3,4,5] ];
static_array[2][1] = 6;
}
The static array has length 2, so index 2 is out of bounds,
must be 0 or 1.
I understand that the index 2 is out of bounds in an array of 2
things. I'm confused about the notation for multidimensional
arrays. I thought that the notation uint[m][n] is read from
right to left, so it denotes n arrays of m things in each
array. So I expected that static_array[k][j] would denotes the
kth element of the jth array.
I find the following rule very straightforward to explaining it.
If you have an array, it's of type `T[]`. The `T` represents the
type of each element. When you access element with index `n` of
this array, it's `arr[n]`, which gives you the `n+1`th `T`
element in the array.
So how do you match this to a static array `ulong[3][2]`? Well,
the `T` in this case is `ulong[3]`, and the array part is `[2]`.
So this is an array of 2 `ulong[3]`.
Therefore, when you index such an array, `static_array[2]` will
get the 3rd element of this 2-element array, and fail.
-Steve