2-dimensional array confusion

spir denis.spir at gmail.com
Mon Nov 1 05:25:37 PDT 2010


On Mon, 1 Nov 2010 07:50:12 +0000 (UTC)
Andreas Kaempf <andreas.kaempf at web.de> wrote:

> 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?

Yes, this is a bit mind disturbing. If you consider each syntax independantly, they are both completely logical:

* type def: since T[2] defines an array of 2 T's, then T[2][3] well defines an array of 3 arrays of 2 T's.

* element access: as your literal [[11,12],[21,22],[31,32]] well shows, the 2-element arrays are the nested ones; so that, to access an element, one must first access a 2-element array -->
	arr[2-element-array-index][element-index]
which maps to
	arr[row-index][element-index]
as one expects.

But the confrontation of both logics is somewhat troubling ;-) To solve it, one would need to reverse the 
array definition format: [n]T means array of n T's, [m][n]T means array of m arrays of n T's.
Simply forget about it and use each syntax according to its own point if view.


Denis
-- -- -- -- -- -- --
vit esse estrany ☣

spir.wikidot.com



More information about the Digitalmars-d-learn mailing list