2-dimensional array confusion

Steven Schveighoffer schveiguy at yahoo.com
Tue Nov 2 06:22:16 PDT 2010


On Mon, 01 Nov 2010 03:50:12 -0400, Andreas Kaempf <andreas.kaempf at web.de>  
wrote:

> Hey folks!
>
> Please enlight me with that prefix notation of 2-dimensional arrays! I
> prepared a snippet giving me headaches:
>
> auto int[2][3] test = [[11,12],[21,22],[31,32]];
> foreach (x, row; test)
> {
> 	Stdout.format("x={}: ", x+1);
>
> 	foreach (y, cell; row)
> 	{
> 		Stdout.format("{}:({}) ", y+1, test[x][y]);
> //		Stdout.format("{}:({}) ", y+1, test[y][x]); // I expected this here
> according the declariation!
> 	}
> 	Stdout.newline;
> }
>
> According to the documentation, the declaration of test should declare 3
> arrays of two ints. The initialization works fine so that's ok for me.
>
> But why do I have to access it with test[x][y] to produce this result?
>
> x=1: y=1:(11) y=2:(12)
> x=2: y=1:(21) y=2:(22)
> x=3: y=1:(31) y=2:(32)
>
> This literally drives me crazy! I really want to understand that!

test is a 3-element array of 2-element arrays.  When you index test, you  
get a 2-element array as its element.  When you index that, you get the  
individual ints.

Think about it in terms of a single array.  If you have an array of type  
T[3], it's element type is T.  So if T is int[2], then it appears  
perfectly consistent:

int[2][3] t; // T is int[2]

auto x = t[0]; // T x is of type int[2]

As an aside, your foreach code is mighty inefficient.  Each time through  
the array, row is a copy of the original element, which means you are  
moving a lot of data without ever using it.

I'd recommend using ref, like this:

foreach(x, ref row; test)

Unfortunately, you can't use the 0..test.length syntax since you are using  
D1.  You could use a straight for-loop.

-Steve


More information about the Digitalmars-d-learn mailing list