Use C struct declaration without knowing definition

Ben Ogles benjaminogles at gmail.com
Thu Jul 25 01:19:22 UTC 2019


On Thursday, 25 July 2019 at 01:08:48 UTC, Ali Çehreli wrote:
> And that type is traded as pointer to some_type on the C API, 
> right? Usually there is a factory method that returns a pointer 
> to a dynamically allocated object.

Correct, there is a function prototype that returns a dynamically 
allocated instance of the struct.

> Indeed. The only option is to use some_type* on the D API as 
> well:
>
> extern(C) {
>   struct S;
>   S* C_API_allocate();
>   void C_API(const(S) *);
> }
>
> // D API
> void foo(S* s)  {
>   C_API(s);
> }
>
> void main() {
>   auto s = C_API_allocate();
>   foo(s);
> }

This makes sense. I can only deal with pointers to dynamically 
allocated instances of the struct since there is no way to know 
the size of the struct at compile time. Now that I think about 
it, this would be the case whether or not I was using D at all.

Thanks for your help!





More information about the Digitalmars-d-learn mailing list