Re: using Unsized Arrays in Structures from d?

2018-05-04 Thread NewUser via Digitalmars-d-learn

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





Re: using Unsized Arrays in Structures from d?

2018-05-04 Thread ag0aep6g via Digitalmars-d-learn

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]`.


Re: using Unsized Arrays in Structures from d?

2018-05-04 Thread NewUser via Digitalmars-d-learn

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


Re: using Unsized Arrays in Structures from d?

2018-05-04 Thread Timoses via Digitalmars-d-learn

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


Re: using Unsized Arrays in Structures from d?

2018-05-04 Thread NewUser via Digitalmars-d-learn

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:

[] ;

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



Re: using Unsized Arrays in Structures from d?

2018-05-04 Thread Timoses via Digitalmars-d-learn

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:

[] ;

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);
}
```


Re: using Unsized Arrays in Structures from d?

2018-05-04 Thread Simen Kjærås via Digitalmars-d-learn

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


using Unsized Arrays in Structures from d?

2018-05-04 Thread NewUser via Digitalmars-d-learn

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
0x00BD
0x00BCA827
0x74118654 in BaseThreadInitThunk
0x77534B17 in RtlGetAppContainerNamedObjectPath
0x77534AE7 in RtlGetAppContainerNamedObjectPath


Regards,
NewUser