array questions

Sergey Gromov snake.scaly at gmail.com
Sun Jan 11 14:34:14 PST 2009


Sun, 11 Jan 2009 17:17:54 -0500, yes wrote:

> Hello again
> 
> is it possible to make a dynamic array less dynamic?
> 
> int[][] array;
> 
> array[0].length = 10; //has to be set at runtime

Um, if that's your code, everything should crash at this point (or throw
in debug mode): array is null, that is, empty, so there is no array[0].

You should array.length = 10; first.  Then you'll get an array of 10
empty arrays of int, and will be able to set their lengths separately:

array[0].length = 10; // OK
assert(array[1].length == 0); // OK

If you want to set up quickly you can write

int[][] array = new int[][](10, 10);

This will give you a sort of square matrix, bit internally it will be an
array of arrays nevertheless.

> also, can this be done?
> 
> int size;
> size = 10; //runtime
> void function( int[size][] array){}

No, static arrays are static, i.e. compile time.


More information about the Digitalmars-d-learn mailing list