d用数组静态初化c构
struct Test {
    int32_t a;
}
struct Test2 {
    int32_t a;
    Test arr[];
}
我想静态初化测试2,然后传递给外部c库.
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);
}
//假定c库按引用传递
数组只是指针,括号/长度仅适用于栈分配的固定长度分配.在我C项目中,我总是用指针来传递数组.
我意思是,取Test2为参数的函数.传递Test2有点怪,没有长度信息.
D按(指针+长度)对定义数组/切片.
 等价的c构应该有:
struct Test2 {
    int32_t a;
    size_t length;//长度类似的
    Test * ptr;
}
但,因为垃集,必须在D端确保生命期.
c中[]等价于D中指针,构上不必有extern(C).
结构并不总是,如果它在C中有固定大小或初化器,就需要匹配D中的大小.否则是的,使用指针.
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号