Thread overview
using Unsized Arrays in Structures from d?
May 04, 2018
NewUser
May 04, 2018
Simen Kjærås
May 04, 2018
Timoses
May 04, 2018
NewUser
May 04, 2018
Timoses
May 04, 2018
NewUser
May 04, 2018
ag0aep6g
May 04, 2018
NewUser
May 04, 2018
Hi,

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
----------------
0x00BCA9D1
0x00BC104C
0x00BD01EB
0x00BD0169
0x00BD0000
0x00BCA827
0x74118654 in BaseThreadInitThunk
0x77534B17 in RtlGetAppContainerNamedObjectPath
0x77534AE7 in RtlGetAppContainerNamedObjectPath


Regards,
NewUser


May 04, 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

The D equivalent is:

struct Item {
    int id;
}

struct Group {
    int i;
    int item_count;
    Item* items;
}

The error message you're getting makes me think items is null. The lack of function names in the stack trace makes it kinda hard to understand exactly what's happening - you can turn that on with -g for DMD. Seeing some of the code that uses these structs might also help.

--
  Simen
May 04, 2018
On Friday, 4 May 2018 at 13:02:08 UTC, NewUser wrote:
> tried defining items[] as both "Item[] items" and "Item* items" in d, it compiles okay but gives an error when trying to access it.

You were on the right track. D array notation is:

<type>[] <identifier>;

For me this works:

```
struct Item
{
  int id;
};

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

void main()
{
    auto g = Group();
    g.items ~= Item(3);
    assert(g.items[0].id == 3);
}
```
May 04, 2018
On Friday, 4 May 2018 at 13:21:53 UTC, Timoses wrote:
> On Friday, 4 May 2018 at 13:02:08 UTC, NewUser wrote:
>> tried defining items[] as both "Item[] items" and "Item* items" in d, it compiles okay but gives an error when trying to access it.
>
> You were on the right track. D array notation is:
>
> <type>[] <identifier>;
>
> For me this works:
>
> ```
> struct Item
> {
>   int id;
> };
>
> struct Group
> {
>   int i;
>   int item_count;
>   Item[] items;
> };
>
> void main()
> {
>     auto g = Group();
>     g.items ~= Item(3);
>     assert(g.items[0].id == 3);
> }
> ```

Hi Timoses,

The structure is being returned from c and I'd like use it from d.

What you have work perfectly when assigning from d.


Regards,
NewUser

May 04, 2018
On Friday, 4 May 2018 at 13:27:03 UTC, NewUser wrote:
> Hi Timoses,
>
> The structure is being returned from c and I'd like use it from d.
>
> What you have work perfectly when assigning from d.
>
>
> Regards,
> NewUser

Then you probably need some `extern(C)` statement introducing the C function and the struct type, right?

You could also try dstep to "translate" the C header file to D:
https://github.com/jacob-carlborg/dstep
May 04, 2018
On Friday, 4 May 2018 at 14:55:18 UTC, Timoses wrote:
> On Friday, 4 May 2018 at 13:27:03 UTC, NewUser wrote:
>> Hi Timoses,
>>
>> The structure is being returned from c and I'd like use it from d.
>>
>> What you have work perfectly when assigning from d.
>>
>>
>> Regards,
>> NewUser
>
> Then you probably need some `extern(C)` statement introducing the C function and the struct type, right?
>
> You could also try dstep to "translate" the C header file to D:
> https://github.com/jacob-carlborg/dstep

Hi Timoses,

Thanks for the suggestion, i'll try it out.

Regards,
NewUser
May 04, 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]`.
May 04, 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