Trying to understand multidimensional arrays in D

Profile Anaysis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 25 21:20:07 PST 2017


On Thursday, 26 January 2017 at 02:29:07 UTC, Ivan Kazmenko wrote:
> On Thursday, 26 January 2017 at 01:47:53 UTC, Profile Anaysis 
> wrote:
>> does this mean that have
>>
>>     int[][4][4] matrix_history;
>>
>> backwards?
>>
>>     int[4][4][] matrix_history;
>>
>> this creates even a more set of problems.
>
> In short, you are right, `int[4][4][]` is a dynamic array of 
> `int[4][4]`.  In turn, `int[4][4]` is a static length-4 array 
> of `int[4]`, and that is a static length-4 array of `int`.  
> It's quite logical once you learn how to read it: if T is a 
> type, then T[] is a dynamic array of that type, and T[4] is a 
> static length-4 array of that type.
>
> So, if I have `int[2][5][7] a;` somewhere, the very last 
> element is `a[6][4][1]`.  If you are inclined to think in terms 
> of this difference, the simple rule of thumb would be that the 
> order of dimensions in the declaration is reversed.

Thanks, knowing the last element is important ; Basically I just 
need to know the proper index. For me, having the array declared 
in symbolic form that matches the indexing, like in C/C++, is 
quicker, easier to remember, and harder to forget. I don't really 
care too much beyond that. They could be declared any way... but 
I find myself getting confused in D because of little things like 
this that don't carry over while almost everything else is.


> Also, note that if you want to have, for example, a dynamic 
> array of 5 dynamic arrays of the same length 7 (modeling a C 
> rectangular array, or a D static array, but with possibility to 
> change the length of each row, as well as the number of rows), 
> you would go with
> `auto a = new int [] [] (5, 7);` (initialization)
> The static array of 5 static arrays of length 7 is still
> `int [7] [5] a;` (type declaration)
> So the reverse only happens in type declarations.
>


> (On the contrary, declarations in C or C++ looks rather 
> unintuitive from this perspective: `T a[4][5][6]` is means that 
> `a` is an array of 4 arrays of 5 arrays of 6 arrays of `T`.  
> Note how we have to read left-to-right but then wrap around the 
> string to get the meaning.)

lol, I don' tknow what the last sentence means. wrap around the 
string? Do you mean look at the variable?

For me the interpretation above is the most logical because it is 
a sequential operation in my mind, if you will. x of y of z and 
the chain can be cut off anywhere and the interpretation still be 
the same.

Since I am a native speaker of English, which is a left to right 
language, it just makes sense. I, am, of coursed biased because I 
started with C/C++ rather than D.



> Additionally, reading about various kinds of arrays in D might 
> help:
> https://dlang.org/spec/arrays.html
>
> And more in-depth material about array slicing:
> http://dlang.org/d-array-article.html
>
> Ivan Kazmenko.




More information about the Digitalmars-d-learn mailing list