How to represent struct with trailing array member

Dibyendu Majumdar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 22 00:39:06 PST 2016


On Friday, 22 January 2016 at 01:53:53 UTC, Chris Wright wrote:
> On Thu, 21 Jan 2016 21:52:06 +0000, Dibyendu Majumdar wrote:
>
>> Hi
>> 
>> I have C code where the struct has a trailing array member:
>> 
>> struct matrix {
>>    int rows;
>>    int cols;
>>    double data[1];
>> };
>> 
> D has bounds checking, which makes this awkward. You would be 
> able to access the data using:
>
>   struct matrix {
>     int rows, cols;
>     double[] data() {
>       void* p = &this;
>       p += this.sizeof;
>       return (cast(double*)p)[0 .. rows * cols];
>     }
>   }
>

Right - I should use slices in other words.

Thanks


More information about the Digitalmars-d-learn mailing list