Hello, folks! Newbie to D, have some questions!
timmyjose via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Feb 20 07:15:47 PST 2017
On Monday, 20 February 2017 at 14:52:43 UTC, ketmar wrote:
> 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. ;-)
Hmmm... yes, that does help indeed. So we read that as "each cell
in an array of 2 cells is an array with 3 cells"? I'll have to
get used to this I suppose!
More information about the Digitalmars-d-learn
mailing list