Assigning to slice of array

Jamie jamieborder01 at gmail.com
Thu Mar 1 21:16:54 UTC 2018


I'm trying to understand arrays and have read a lot of the 
information about them on this forum. I think I understand that 
they are set-up like Type[], so that int[][] actually means an 
array of int[].

I create an array as per the following:
     auto arr = new int[3][2][1];
which produces:
     [[0, 0, 0], [0, 0, 0]]
(for each of the following assignments, assume that the array is 
set back to zeros)

and I can change the 2nd element of the 1st array using:
     arr[0][1] = 4;
which produces:
     [[0, 4, 0], [0, 0, 0]]

and I can change the entire 1st array using:
     arr[0][0 .. 3] = 5;
which produces:
     [[5, 5, 5], [0, 0, 0,]]

however when I try and change elements across arrays rather than 
within arrays my understanding breaks down.. when I try
     arr[0 .. 2][0] = 3;   // which I think is equivalent to 
arr[0][0] and arr[1][0]
I'm expecting:
     [[3, 0, 0], [3, 0, 0]]
but it produces:
     [[3, 3, 3], [0, 0, 0]]
showing that arr[0][0 .. 2] is making the same index as arr[0 .. 
3][0] ?

Instead of using [0 .. 2] I can use the actual indices to get the 
result I desired:
     arr[0][0] = 3;
     arr[1][0] = 3;
which produces:
     [[3, 0, 0], [3, 0, 0]]

Could I just get some help with understanding how the slice [0 .. 
2] actually works? Thanks


More information about the Digitalmars-d-learn mailing list