digitalmars.D.learn - using Unsized Arrays in Structures from d?
- NewUser (29/29) May 04 2018 Hi,
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (17/33) May 04 2018 The D equivalent is:
- Timoses (22/25) May 04 2018 You were on the right track. D array notation is:
- NewUser (6/31) May 04 2018 Hi Timoses,
- ag0aep6g (11/27) May 04 2018 In the C code, the elements of `items` are directly part of the
- NewUser (8/39) May 04 2018 Hi ag0aep6g,
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 ViolationThe 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: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, NewUsertried 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: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, NewUserThen 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, Thanks for the suggestion, i'll try it out. Regards, NewUserHi 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, NewUserThen 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 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 ViolationIn 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: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, NewUserHow 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 ViolationIn 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