How to handle char* to string from C functions?

bearophile bearophileHUGS at lycos.com
Thu Jan 2 09:52:54 PST 2014


Gary Willoughby:

> I've noticed that const(char)** can be accessed via indexes:
>
> writefln("%s", pp[0].to!(string)); //etc.
>
> cool!

This is a feature that works with all pointers to a sequence of 
items, like in C. But array bounds are not verified, so it's more 
bug-prone. So if you know the length it's better to slice the 
pointer as soon as possible, and then use the slice:

auto ap = pp[0 .. N];
writefln("%s", ap[0].text);

Or just:

printf("%s\n", ap[0]);

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list