C style array declaration.

ag0aep6g anonymous at example.com
Mon Mar 26 10:49:22 UTC 2018


On 03/26/2018 12:16 PM, Aedt wrote:
> I'm a big fan of betterC. In C, you can initialize an array without 
> specifying the length like this
> int ia[ ] = {0, 2, 1};
> 
> What is the translation of this?

The language doesn't have that feature. But there's a PR to add 
`staticArray` to the standard library [1]. When that gets in, you can write:

     import std.array;
     auto ia = [0, 2, 1].staticArray; /* ia is an int[3] */

[...]
> Also, is it possible to retrieve the pointer of the sequence of actual 
> data from std.container.array? If all fails I'd like to use this container.

Take the address of the first element:

     import std.container.array: Array;
     Array!int a = [1, 2, 3];
     int* p = &a[0];
     assert(*p == 1);
     assert(*++p == 2);
     assert(*++p == 3);

Be aware that the pointer potentially becomes invalid when you append to 
the array.


[1] https://github.com/dlang/phobos/pull/6178


More information about the Digitalmars-d-learn mailing list