Converting a string[] to char**

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 8 22:52:28 PDT 2017


On Tuesday, 9 May 2017 at 05:38:24 UTC, ag0aep6g wrote:

> You have to create a new array of pointers. As rikki cattermole 
> has pointed out, you also have to null-terminate the individual 
> strings, and pass the amount of pointers in a separate 
> parameter.
>
> ----
> import std.algorithm.iteration: map;
> import std.array: array;
> import std.conv: to;
> import std.string: toStringz;
>
> string[] strs = ["foo", "bar", "baz"];
>
> /* convert string[] to char*[]: */
> immutable(char)*[] chptrs = strs.map!toStringz.array;
>
> immutable(char)** ppEnabledLayerNames = chptrs.ptr;
> uint enabledLayerCount = chptrs.length.to!uint;
> ----

Although, if it's known that the array was populated with 
literals, toStringz isn't needed. String literals are 
automatically nul terminated.


More information about the Digitalmars-d-learn mailing list