setting array dimensions at runtime

user at domain.invalid user at domain.invalid
Sun Dec 5 10:38:53 PST 2010


On 5-12-2010 19:20, Matthias Walter wrote:
>
>> The only thing I've been able to think of is
>>
>>     byte[][] a;
>>     a.length = size;
>>     for (int i; i<  size; i++) {
>>          a[i].length = size;
>>     }
> Well, you can do at least:
>
> auto a = new byte[][size];
> foreach (ref row; a)
>    row = new byte[size];

That works.

So I can write

    byte[] a = new byte[size];

and

    byte[][] a = new byte[][size];

but not

    byte[][] a = new byte[size][];

or

    byte[][] a = new byte[size][size];

let alone

    byte[size][size] a;


BTW, somebody on stackoverflow just posted an alternative
that comes closest to what I was looking for.

    byte[][] a = new byte[][](size, size);

I saw this notation before but I can't remember where.

Hey, I love D but it can be pretty confusing sometimes :)

Thanks


More information about the Digitalmars-d-learn mailing list