what's the right way to get char* from string?

Alex Parrill via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 5 17:35:31 PDT 2016


On Thursday, 5 May 2016 at 07:49:46 UTC, aki wrote:
> extern (C) int strcmp(char* string1, char* string2);

This signature of strcmp is incorrect. strcmp accepts const char* 
arguments [1], which in D would be written as const(char)*. The 
immutable(char)* values returned from toStringz are implicitly 
convertible to const(char)* and are therefore useable as-is as 
arguments to strcmp.

import std.string;
extern (C) int strcmp(const(char)* string1, const(char)* string2);
auto v = strcmp(somestring1.toStringz, somestring2.toStringz);

[1] http://linux.die.net/man/3/strcmp


More information about the Digitalmars-d-learn mailing list