Simple array init question

Chris Nicholson-Sauls ibisbasenji at gmail.com
Sat Apr 7 11:39:26 PDT 2007


Graham wrote:
> Thanks for your reply.  No, I didn't try that, since the array I'm 
> actually using is several thousand elements large.
> 
> 
> Paul Findlay wrote:
>>> It compiles, but if I enter:
>>>    int[5][5] buffer;
>>>    buffer[][] = 5;
>>
>> Have you tried:
>>
>> int[5][5] buffer;
>> buffer[] = [5,5,5,5,5]; // or something
>>
>> (Or try in the digitalmars.D.learn newgroup)
>>
>>  - Paul

In which case you've got two immediate options, and a third option requiring a library.  :)

Immediate #1 - Use a loop (possibly wrapped in a function) to init the array.

Immediate #2 - Use a typedef of the array's element type with a custom init value -- 
unfortunately this is rarely realistic as typedef types are strong in D.

Library option - Use Cashew (cashew.utils.Array) thusly:
int[1024][1024] buf = repeat(repeat(5, 1024_U), 1024_U);

(Admittedly untested, but it should work fine.  Cashew is available from DSource.)
http://www.dsource.org/projects/cashew


When it comes right down to it, I actually have to admit to wishing the '[][] = 5' 
approach worked.  Hm.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list