string from C function

Andy Valencia dont at spam.me
Thu May 8 04:51:27 UTC 2025


On Thursday, 8 May 2025 at 00:53:20 UTC, Mike Parker wrote:
>> tst44.d(6): Error: cannot implicitly convert expression 
>> `fromStringz(ctime(null))` of type `char[]` to `string`
> `fromStringz` is giving you a slice of a `char*`, typed 
> `char[]`.
>
> `string` is `immutable(char)[]`, so you can't assign `char[]` 
> to it.
>
> You could:
>
> * change the type of `s` to `char[]`
> * call `.idup` on the array returned from `fromStringz` to 
> allocate a `string`
> * use `std.conv.to`

Thank you.  I want to work in strings, so the first one's not an 
option.  But both the second and third do the trick.  Would you 
say the to!string would be the most idiomatic?  It worked as 
"to!string(ctime(&t))", but is it safe to assume it's reliably 
dealing with null-terminated strings?  It's the pithiest if so.

Andy




More information about the Digitalmars-d-learn mailing list