Doubt - Static multidimension arrays

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 18 21:32:07 PST 2016


On 01/18/2016 07:20 PM, albert00 wrote:

 > It's strange since I was declaring int[1][2] (1 row / 2 columns) and
 > then accessing as:
 >
 >      arr2[0][0] = 1;
 >      arr2[1][0] = 2;
 >
 > Seems like 2 rows and 1 column. This makes sense?

Yes, it makes sense and its consistent. This is one of many little 
things that D is an improvement over C and C++.

Static array type syntax is always the following:

     Type[length]

For example, the following is a row with two columns:

     int[2]

When you want a certain number of those, you follow the same array 
definition syntax:

     Type[length]

Since each element is int[2] in this case, for 3 rows we get:

     int[2][3]

So, in order to get 1 row of 2 columns, you would write

     int[2][1]

Accessing elements is consistent as well:

     var[index]

So,

     arr2[0]

would be the first row, which has two columns; which is further accessed 
by another index:

     arr2[0][1]

is first row, second column.

Ali



More information about the Digitalmars-d-learn mailing list