Dynamic multidimensional arrays
Jonathan M Davis
jmdavisProg at gmx.com
Tue Jul 5 00:00:15 PDT 2011
On 2011-07-04 23:56, Jonathan M Davis wrote:
> 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.
Correction,
auto anArray = new char[4][5];
would create a dynamic array of length 5 of static arrays with length 4,
though
auto anArray = new char[][](4, 5);
does create a dynamic array of length 4 of dynamic arrays of length 5. It
quickly gets confusing when dealing with dimensions and static arrays IMHO.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list