On Sunday, 20 July 2014 at 16:00:41 UTC, Daniel Gibson wrote:
Hi,
I have a C variadic function (passed from C code into my D code
via function pointer) that I need to call with a static array.
So according to the D documentation, static arrays are passed
by value in D2 and by reference in C and D1.
(Even though http://dlang.org/abi.html claims "Static arrays
are passed as pointers to their first element." - I guess this
just wasn't updated for D2)
For "normal" functions http://dlang.org/interfaceToC.html tells
me to add a "ref" in the function signature, to tell D to pass
it by reference (couldn't this be implicit for extern (C)
functions?) - But I obviously can't to this for vararg function
arguments.
So let's imagine the following code:
extern (C) alias funPtr_t = ptrdiff_t function( ptrdiff_t
arg, ... );
funPtr_t fun = ...; // is assigned somewhere..
void bla( float[3] v ) {
fun( 42, v );
}
This produces the following compiler error (DMD 2.065 linux
amd64):
"Error: cannot pass static arrays to extern(C) vararg functions"
/However/, if I typedef a float[3] type, the compiler does not
complain (not sure if the code behaves like expected, though,
or if it's still passed by value instead of by reference as
expected by the C code):
typedef float[3] vec3_t;
void bla( vec3_t v ) {
fun( 42, v );
}
Then again, if I use alias instead of the deprecated typedef:
alias vec3_t = float[3];
I again get "Error: cannot pass static arrays to extern(C)
vararg functions".
Is there a "proper" way to make this work?
If not, any ideas for a viable workaround?
Cheers,
Daniel
C functions takes arrays by pointer to first element. fun(42,
v.ptr) should work.