On Sunday, 15 August 2021 at 08:56:07 UTC, rempas wrote:
On Sunday, 15 August 2021 at 08:53:50 UTC, Tejas wrote:
External C libraries expect strings to be null terminated, so
if you do use `.dup`, use `.toStringz` as well.
Yeah, yeah I got that. My question is, if I should avoid
`cast(char*)` and use `.toStringz` while both do the exact same
thing?
They don't do the same thing. toStringz always copies, always
GC-allocates, and always NUL-terminates. `cast(char*)` only does
what you want in the case that you're applying it a string
literal. But in that case you shouldn't cast, you should just
```d
const char* s = "John";
```
If you need cast cast the const away to work with a C API, doing
that separately, at the point of the call to the C function,
makes it clearer what you're doing and what the risks are there
(does the C function modify the string? If so this will segfault).