C Style char**

Doug Clayton dougclayton0242 at gmail.com
Mon May 13 13:08:31 UTC 2019


On Monday, 13 May 2019 at 09:56:19 UTC, evilrat wrote:
> On Monday, 13 May 2019 at 09:24:34 UTC, Doug Clayton wrote:
>> [...]
>
> You don't need to cast it, arrays have .ptr property to get 
> pointer to first element, and in @safe you just &arr[0] 
> instead, or so I think.
>
> If you are using static/literal arrays and know what you are 
> doing this will work.
>
> Otherwise it safer to just use regular string[] array and use 
> something like that
>
>
>     const(char*)* toCStringList(string[] arr)
>     {
>         import std.string; // toStringz() - adds null 
> terminator, allocates using GC if necessary
>         import std.array; // array() - eagerly converts range 
> to array, allocates using GC
>         import std.algorithm; // map() - apply function to range
>         return arr
>             .map!(toStringz)
>             .array();
>     }

Thanks so much! That's much more elegant than what I was thinking 
I would have to use.



More information about the Digitalmars-d-learn mailing list