www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - static init c struct with array filed

reply test <test123 gmail.com> writes:
```c
struct Test {
     int32_t a;
}
struct Test2 {
     int32_t a;
     Test arr[];
}
```

I need static const init Test2, then pass it to c library 
late(third library, can not change the type def).


I am not sure how to do it in D.
Mar 16 2022
next sibling parent reply user1234 <user1234 12.de> writes:
On Wednesday, 16 March 2022 at 07:27:06 UTC, test wrote:
 ```c
 struct Test {
     int32_t a;
 }
 struct Test2 {
     int32_t a;
     Test arr[];
 }
 ```

 I need static const init Test2, then pass it to c library 
 late(third library, can not change the type def).


 I am not sure how to do it in D.
```d extern(C) struct Test { int a; } extern(C) struct Test2 { int a; Test* arr; } static const Test2 t2 = Test2(2, null); void main(){ to_c_library(&t2); to_c_library(new Test2(2, null)); Test2 local; to_c_library(&local); } ``` assuming the c library takes by reference
Mar 16 2022
parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
On Wednesday, 16 March 2022 at 11:27:20 UTC, user1234 wrote:
 assuming the c library takes by reference
My experience all arrays are effectively just pointers, and the brackets/length is only really applicable to stack allocated fixed length allocations. In my own C projects i always used pointers to pass arrays around. There's also static construction, though i don't see how that improves anything https://dlang.org/spec/module.html#staticorder
Mar 16 2022
parent reply user1234 <user1234 12.de> writes:
On Wednesday, 16 March 2022 at 14:42:02 UTC, Era Scarecrow wrote:
 On Wednesday, 16 March 2022 at 11:27:20 UTC, user1234 wrote:
 assuming the c library takes by reference
My experience all arrays are effectively just pointers
I meant the function that takes the Test2 as parameter, but to be honest I dont get exactly what does OP want actually... that's true that passing a Test2 is somewhat strange as there's no info for the array length.
Mar 16 2022
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 3/16/22 10:03, user1234 wrote:
 On Wednesday, 16 March 2022 at 14:42:02 UTC, Era Scarecrow wrote:
 On Wednesday, 16 March 2022 at 11:27:20 UTC, user1234 wrote:
 assuming the c library takes by reference
My experience all arrays are effectively just pointers
I meant the function that takes the Test2 as parameter, but to be honest I dont get exactly what does OP want actually...
They want to pass Test2 to C and be able to use it. D ABI defines arrays (slices) as a pair of size_t and a pointer: https://dlang.org/spec/abi.html#arrays I haven't passed such a struct to C but I believe the equivalent C struct can assume there are the following members in place of the array: struct Test2 { int32_t a; size_t length; Test * ptr; } However, the lifetime of ptr must be ensured on the D side because utless there is a reference to it on the D side, the GC can free the memory at any time and C side may access invalid memory. That is explained here: https://dlang.org/spec/interfaceToC.html#storage_allocation Ali
Mar 16 2022
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Wednesday, 16 March 2022 at 07:27:06 UTC, test wrote:
 ```c
 struct Test {
     int32_t a;
 }
 struct Test2 {
     int32_t a;
     Test arr[];
 }
 ```

 I need static const init Test2, then pass it to c library 
 late(third library, can not change the type def).
Any time you see a '[]' in C, the equivalent declaration in D has to be a pointer. Like Ali said, D's arrays are a pointer and length pair, so declaring the declaration of the `arr` member will not match the declaration on the C side. Since you say you can't change the declaration on the C side, user1234's answer is the direction you want to go (though you don't need the `extern(C)` on the structs).
Mar 16 2022
parent Adam Ruppe <destructionator gmail.com> writes:
On Thursday, 17 March 2022 at 00:16:39 UTC, Mike Parker wrote:
 On Wednesday, 16 March 2022 at 07:27:06 UTC, test wrote:
 ```c
 struct Test {
     int32_t a;
 }
 struct Test2 {
     int32_t a;
     Test arr[];
 }
 ```

 I need static const init Test2, then pass it to c library 
 late(third library, can not change the type def).
Any time you see a '[]' in C, the equivalent declaration in D has to be a pointer.
Not always with structs....... if it has a fixed size or an initializer in C, then you need to match the size in D. Otherwise yeah use the pointer.
Mar 16 2022