Interfacing C programs: Pass D string to C function

Mike Parker aldacron at gmail.com
Fri Dec 13 02:06:21 PST 2013


On 12/13/2013 5:00 PM, Dfr wrote:
> Hello
>
> I trying to write simple wrapper around pcre and have problem passing
> strings to it. As i understood, the best way is std.string.toStringZ.
>
> So, my code look like:
>
> string pattern = "....";
> pcre_compile2( toStringz(pattern), options, &errcode, &errmsg,
> &erroffset, cast(char*)null);
>
> This gives me error:
>
> Error: function pcre_compile2 (char*, int, int*, char**, int*, char*) is
> not callable using argument types (immutable(char)*, int, int*, char**,
> int*, char*)
>
> Any ideas for better way to do this task ?

Declare the function to like so:

pcre* pcre_compile2( const(char)*, int, int*, const(char)**, int*, 
const(ubyte)*);

This matches the C declaration according to [1]. immutable args can be 
passed to const params without seeing your error. Also, note the last 
paramters. In C, it's declared to be const unsigned char* and the 
documentation suggest it's intended to be treated as a byte array rather 
than a string. In that case, const(ubyte)* is the appropriate choice. D 
declarations of C functions should generally match the C version as 
closely as possible, including use of const, but with intent taken into 
account as well (e.g. byte array vs string).

[1] 
https://code.google.com/p/wiimc/source/browse/trunk/libs/pcre/doc/html/pcre_compile2.html?r=423


More information about the Digitalmars-d-learn mailing list