Multi-dimensional fixed arrays

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 30 13:33:30 PDT 2015


On Tuesday, 30 June 2015 at 20:17:12 UTC, Justin Whear wrote:
> No.  The order of braces when indexing is the opposite of the 
> order when
> declaring.
> The declaration
>> int [1][2] foo;
> reads innermost to outermost, "((int [1] ) [2])"
>
> When indexing foo, you index from outermost to innermost, so
>> foo[1]
> means the second one-element array and
>> foo[1][0]
> means the first element of the second one-element array.

I think this is a good explanation.

Looking through
http://dlang.org/arrays.html
I see that the multidimensional array indexing is not 
particularly focused on (could be improved?). I tend to prefer 
reasoning things through than relying on a rule (more likely to 
forget the rule). Thus, I would recommend the OP looks at the way 
they describe the prefix array declarations for multidimensional 
arrays. They have the example
int[4][3] b;  // array of 3 arrays of 4 ints each
So you can think of b as an array containing 3 arrays with 4 ints 
each. For the OP's foo, he should think of foo as an array 
containing 2 arrays with 1 int each. Moreover, it's more likely 
that you want to index the arrays and then what's in the arrays, 
i.e. it's more likely that you would want to do something with 
the first array of foo and then the second array of foo. This 
notation makes it a little bit easier to do that.


More information about the Digitalmars-d-learn mailing list