exern (C) linkage problem

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Tue Jul 20 01:24:18 PDT 2010


On Mon, 19 Jul 2010 18:01:25 -0400, bearophile wrote:

>> typedef char* Cstring;
>> extern(C) Cstring strcmp(Cstring s1, Cstring s2); ...
> 
> You can use just a struct too:
> 
> import std.string: toStringz;
> 
> struct Cstring {
>     const(char)* ptr;
> }
> 
> extern(C) Cstring strcmp(Cstring s1, Cstring s2);
> 
> Cstring toCString(T)(T[] s) {
>     return Cstring(toStringz(s));
> }
> void main() {
>     auto s1 = "abba";
>     auto s2 = "red";
>     // auto r = strcmp(toCString(s1), s2); // compile error auto r =
>     strcmp(toCString(s1), toCString(s2)); // OK
> }
> 
> Bye,
> bearophile

Good point.  Actually, I think there should be a CString type in Phobos, 
but I think it should wrap a ubyte*, not a char*.  The reason for this is 
that D's char is supposed to be a UTF-8 code unit, whereas C's char can 
be anything.

-Lars


More information about the Digitalmars-d-learn mailing list