January 02, 2014 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Thursday, 2 January 2014 at 15:31:25 UTC, bearophile wrote:
> Gary Willoughby:
>
>> Another question: how do i convert const(char)** to string[]?
>
> If you know that you have N strings, then a solution is (untested):
>
> pp[0 .. N].map!text.array
>
> If it doesn't work, try:
>
> pp[0 .. N].map!(to!string).array
>
> Bye,
> bearophile
Thanks, both work well:
I've noticed that const(char)** can be accessed via indexes:
writefln("%s", pp[0].to!(string)); //etc.
cool!
|
January 02, 2014 Re: How to handle char* to string from C functions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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
|
Copyright © 1999-2021 by the D Language Foundation