using Unsized Arrays in Structures from d?

ag0aep6g anonymous at example.com
Fri May 4 15:37:28 UTC 2018


On Friday, 4 May 2018 at 13:02:08 UTC, NewUser wrote:
> How can I use the following c structure from d.
>
> struct Item
> {
>   int id;
> };
>
> struct Group
> {
>   int i;
>   int item_count;
>   struct Item items[];
> };
>
> tried defining items[] as both "Item[] items" and "Item* items" 
> in d, it compiles okay but gives an error when trying to access 
> it.
>
> Here is the error.
> object.Error@(0): Access Violation

In the C code, the elements of `items` are directly part of the 
struct. There is no indirection. D doesn't have dedicated syntax 
for this, but you can hint at it with a zero-sized array:

struct Group
{
     int i;
     int item_count;
     Item[0] items;
}

Then access an item with `group.items.ptr[index]`.


More information about the Digitalmars-d-learn mailing list