using Unsized Arrays in Structures from d?
NewUser
newuser at newuser.com
Fri May 4 16:02:34 UTC 2018
On Friday, 4 May 2018 at 15:37:28 UTC, ag0aep6g wrote:
> 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]`.
Hi ag0aep6g,
Thanks a lot for that.
I should have thought of that (i would still have missed the .ptr
part), the old c syntax for the same thing used to be "Item
items[0]".
Thanks,
NewUser
More information about the Digitalmars-d-learn
mailing list