C string to D without memory allocation?

Jakob Ovrum via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Dec 20 21:56:12 PST 2015


On Monday, 21 December 2015 at 05:39:32 UTC, Rikki Cattermole 
wrote:
> size_t strLen = ...;
> char* ptr = ...;
>
> string myCString = cast(string)ptr[0 .. strLen];
>
> I can't remember if it will include the null terminator or not, 
> but if it does just decrease strLen by 1.

Strings from C libraries shouldn't be casted to immutable. If the 
characters of the C string are truly immutable, mark it as 
immutable(char)* in the binding (and needless to say, use 
fromStringz instead of explicit counting when the length isn't 
known).


More information about the Digitalmars-d-learn mailing list