Strange behavior of array

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 15 21:39:57 PDT 2015


On Friday, 16 October 2015 at 03:01:12 UTC, VlasovRoman wrote:

> Oh, thank you. Some strange solution.

D doesn't have multidimensional built-in arrays, but rectangular 
arrays. Think of it this way:

int[3] a1;

a1 is a static array of 3 ints. Indexing it returns an int. We 
can think of it like this:

(int)[3]

On the same lines:

int[3][4] a2;

a2 is a static array of 4 static arrays of 3 ints. In other words:

(int[3])[4].

Therefore, int[0] returns the first int[3], int[1] the second, 
and so on.

int[0][1] returns the second element of the first int[3].

Rikki's solution to your problem was to reverse the indexes when 
reading the array. But if you want to index it just as you would 
in C or C++, you should reverse the indexes in the declaration. 
Where you declare int[rows][columns] in C, you would declare 
int[columns][rows] in D, then reading from them is identical.




More information about the Digitalmars-d-learn mailing list