Zero length arrays in D
Eduard Staniloiu
edi33416 at gmail.com
Mon Feb 18 13:34:37 UTC 2019
Hello
According to the spec[0], D supports zero length arrays [1].
I have given this a shot at https://run.dlang.io/is/PwbPxJ
Attempting to use the zero-length array results in a compiler
error
`a.contents[2]` -> Error: array index 2 is out of bounds
(*a).contents[0 .. 0]
The way I've used around this error is "safely" break/trick the
type system
```
int *p = a.contents.ptr;
int[] p_cont = p[0 .. n];
p_cont[2] = 10; // fine
```
Is this the intended way of working with zero length arrays?
Cheers,
Edi
[0] - https://dlang.org/spec/arrays.html#static-arrays, best
practices pt 2
[1] -
https://gcc.gnu.org/onlinedocs/gcc-4.7.4/gcc/Zero-Length.html
More information about the Digitalmars-d-learn
mailing list