Calling a C Function with C-style Strings

Dicebot public at dicebot.lv
Sat Nov 30 07:58:41 PST 2013


On Saturday, 30 November 2013 at 15:48:28 UTC, Nordlöw wrote:
> I think I've read somewhere that string literals (`const` or 
> `immutable`) are implicitly convertible to zero 
> (null)-terminated strings.

AFAIR, this should work:

char[] name = "alpha".dup;
mktemp(name.ptr);

D literals are zero-terminated so toStringz is only needed if you 
want to pass to C function slice of literal or some runtime input.

Another option is to declare extern(C) function to take `const 
(char)*` argument, as far as I remember C it should not affect 
mangling but will allow to pass literal directly without 
allocating a mutable copy.


More information about the Digitalmars-d-learn mailing list