timmyjose wrote:
Suppose I have a simple 2 x 3 array like so:
import std.stdio;
import std.range: iota;
void main() {
// a 2 x 3 array
int [3][2] arr;
foreach (i; iota(0, 2)) {
foreach(j; iota(0, 3)) {
arr[i][j] = i+j;
}
}
writefln("second element in first row = %s", arr[0][1]);
writefln("third element in second row = %s", arr[1][2]);
writeln(arr);
}
My confusion is this - the declaration of the array is arr
[last-dimension]...[first-dimension], but the usage is
arr[first-dimension]...[last-dimension]. Am I missing something here?
yes. it is quite easy to remember if you'll just read the declaration
from left to right:
int[3][2] arr
becomes:
(int[3])[2]
i.e. "array of two (int[3]) items". no complicated decoding rules.
and then accessing it is logical too: first we'll index array of two
items, then `(int[3])` array.
declaration may look "reversed", but after some time i found it
straightforward to read. ;-)