Question

I would like convert an array in D of the form:

string[] arrayStr = [ "hi, "is fun", "use D programming" ];

I have a C function which takes a C variadic:

void c_func( const char* format, ... );

I could do:

foreach(str; arrayStr)
    func( str );

But this seems like hacking the C variadic and I would rather convert directly to C vararg,

How can I do this?

thanks

Was it helpful?

Solution

A D array is a struct consisting of length and pointer. For instance, you can pass a D string to printf by formatting it as "%.*s".

So if you pass your array to the C function's variadic argument, it will find a long int (length) and a char[]* (ptr) in its varargs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top