Doubt - Static multidimension arrays

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 18 23:19:54 PST 2016


On 01/18/2016 11:12 PM, Albert00 wrote:
 > On Tuesday, 19 January 2016 at 05:32:07 UTC, Ali Çehreli wrote:
 >
 > Ali, look what you said:
 >
 >> For example, the following is a row with two columns:
 >>
 >>     int[2]
 >
 > Then you said:
 >
 >> So, in order to get 1 row of 2 columns, you would write
 >>
 >>     int[2][1]

To rephrase myself, that is an array of 1 element, where the element 
type is int[2]. So the only valid index is 0, which gives you an element 
of int[2]. (You can further index into that element of course.)

 > So the first pair of square-brackets is the column and second is the row
 > as you said above,

I stress the fact that it is always the following syntax:

     Type[length]

So, again, if we have an array of 1-element where the elements are of 
type int[2], then it is this (space added for readability):

     int[2] [1]

 > but look what happens when I try to access thinking
 > that way:

I suspect C and C++ way for inside-out (or is it outside-in) syntax is 
affecting your thinking. ;)

 > void main(){
 >      int[2][1] arr; // 2 columns & 1 row as Ali said...
 >
 >      arr[0]

That one gives you the first element.

 > [0] = 1;

and that one gives you the first element of that first element.

 >      arr[1][0] = 2;

Sorry, there is no element-1 for the arr: That has only one element.

 > }
 >
 > ERROR:
 >
 > /d609/f167.d(14): Error: array index 1 is out of bounds arr[0 .. 1]
 > /d609/f167.d(14): Error: array index 1 is out of bounds arr[0 .. 1]
 >
 >
 > So now the first pair of brackets in fact is the ROW and the second is
 > the COLUMN, because this works:
 >
 > void main(){
 >      int[2][1] arr; // 2 columns & 1 row

Yes, that's exactly what I said. :)

 >      arr[0][0] = 1;
 >      arr[0][1] = 2;
 > }
 >
 > Maybe I'm really dumb,

Not at all. I blame C and C++. ;)

 > but you need to agree that even with your good explanation it
 > still doesn't making sense.

I don't agree: It makes sense and is consistent. :)

 > Albert.

Ali



More information about the Digitalmars-d-learn mailing list