Converting a string[] to char**

David Zhang via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 9 00:50:33 PDT 2017


On Tuesday, 9 May 2017 at 05:52:28 UTC, Mike Parker wrote:
> 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.

Thanks for all your answers.

The strings are all predefined statically for the moment, with a 
'\0' character at the end.

I take from this that there's no way to avoid allocating then? I 
had hoped... :(

If indeed there is no way to avoid allocation, do the allocations 
have to remain 'alive' for the duration of the instance? Or can I 
deallocate immediately afterwards? I can't seem to find it in the 
Vulkan spec.



More information about the Digitalmars-d-learn mailing list