translate C struct char array into D

workman workman at gmail.com
Mon Aug 2 10:05:12 UTC 2021


On Friday, 30 July 2021 at 21:53:48 UTC, russhy wrote:
> On Friday, 30 July 2021 at 14:05:58 UTC, workman wrote:
>> I get want to define this struct in D:
>>
>> ```c
>> struct test1 {
>>     struct test1 *prev;
>>     struct test1 *next;
>>     size_t v1;
>>     size_t v2;
>>     size_t v3;
>>     char data[];
>> };
>> ```
>>
>> ```d
>> struct test1 {
>>     test1 *prev;
>>     test1 *next;
>>     size_t v1;
>>     size_t v2;
>>     size_t v3;
>>     char* data;
>> };
>> ```
>>
>> when I compare the size, test1.sizeof is 48 and sizeof(struct 
>> test1) from C is 40.
>>
>> Anyone can explain what should I do with this ?
>>
>> If I use test1 as member of other struct, the total size will 
>> not match.
>
>
> ```d
> struct test1 {
>     test1 *prev;
>     test1 *next;
>     size_t v1;
>     size_t v2;
>     size_t v3;
>     char[0] data;
> }
> ```
>
> data.ptr to access its pointer

Thanks for you all for the explain and tips, I will try avoid 
embed this struct into parent.


More information about the Digitalmars-d-learn mailing list