Dynamic multidimensional arrays

Jonathan M Davis jmdavisProg at gmx.com
Mon Jul 4 23:56:27 PDT 2011


On 2011-07-04 23:42, Bellum wrote:
> Can anyone point me in the right direction for doing something like this
> in D:
> 
>      char[][] anArray;
>      int rows, cols;
> 
>      ...
> 
>      anArray = new char[rows][cols];
> 
> It isn't possible in this way because "rows cannot be read at compile
> time", which seems to me to be the point of dynamic arrays. :P

auto anArray = new char[][](rows, cols);

Putting the numbers directly in the brackets tries to create a static array 
once you get beyond the first dimension. So,

auto anArary = new char[4][5];

would create a dynamic array of length for with elements which are static 
arrays of length 5. If you want it to by dynamic all the way, you need to put 
the dimensions in the parens like above. Personally, I _never_ put them in the 
brackets, even when the dynamic array has just one dimension. It's just 
simpler to always put them in the parens and not worry about it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list