string from C function

Mike Parker aldacron at gmail.com
Thu May 8 00:53:20 UTC 2025


On Wednesday, 7 May 2025 at 22:47:35 UTC, Andy Valencia wrote:

> So about fromStringz...
>
> ```d
> import std.string : fromStringz;
> import core.stdc.time : ctime;
>
> void
> main() {
>     string s = fromStringz(ctime(null));
> }
> ```
>
> Like that?
>
> tst44.d(6): Error: cannot implicitly convert expression 
> `fromStringz(ctime(null))` of type `char[]` to `string`
>
> Still seeking...

`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`


More information about the Digitalmars-d-learn mailing list