exern (C) linkage problem
    bearophile 
    bearophileHUGS at lycos.com
       
    Tue Jul 20 02:10:47 PDT 2010
    
    
  
In that code, for further safety, I'd like to make it not possible (without a cast) code like this (here toStringz doesn't get called):
strcmp(Cstring(s1.ptr), Cstring(s2.ptr));
So I think this code is a bit better:
import std.string: toStringz;
struct Cstring {
    const(char)* ptr; // const(ubyte)* ?
    static Cstring opCall(string s) {
        Cstring cs;
        cs.ptr = toStringz(s);
        return cs;
    }
}
extern(C) Cstring strcmp(Cstring s1, Cstring s2);
void main() {
    auto s1 = "abba";
    auto s2 = "red";
    auto r2 = strcmp(Cstring(s1), Cstring(s2));
}
Lars T. Kyllingstad:
> 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.
Right. But toStringz() returns a const(char)*, so do you want to change toStringz() first?
Bye,
bearophile
    
    
More information about the Digitalmars-d-learn
mailing list