Search code examples
cd

How to use D structures from C code?


Could anyone explain me how to use D structures from C code? If I am trying to use it I receive such an error:

error: storage size of 'myStruct' isn't known
   struct str_struct myStruct;

This is a structure:

extern(C) {
    struct str_struct {
        string str;
      };
}

I use it in C like this : struct str_struct myStruct;


Solution

  • You have to duplicate the struct definition with all members in both languages (unless you want to refer to it only by pointer). C can't see a field list written in D.

    D:

    struct Foo {
        int length;
        int* data;
    }
    

    C:

    typedef struct Foo {
        int length;
        int* data;
    };
    

    The tricky thing is to get long right. long in D is always 64 bits, so in C, that would be long long. Most other basic types translate pretty easily though: short=>short, int to int, char to char, pointers work the same way, etc.