www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C Style char**

reply Doug Clayton <dougclayton0242 gmail.com> writes:
Hi All,

First time poster :)

I'm working on getting a binding to Vulkan working properly in D, 
but I've run into a roadblock. The good news is that the binding 
seems to work fine, but I'm having an issue getting a parameter 
that needs an input of a const(char*)* to receive all of the 
members of the array. It only will receive the first member.

I've looked through the documentation, but I haven't seen a clear 
way to cast a char*[] to a char** without it losing the rest of 
the members. Can anyone help?
May 13 2019
parent reply evilrat <evilrat666 gmail.com> writes:
On Monday, 13 May 2019 at 09:24:34 UTC, Doug Clayton wrote:
 Hi All,

 First time poster :)

 I'm working on getting a binding to Vulkan working properly in 
 D, but I've run into a roadblock. The good news is that the 
 binding seems to work fine, but I'm having an issue getting a 
 parameter that needs an input of a const(char*)* to receive all 
 of the members of the array. It only will receive the first 
 member.

 I've looked through the documentation, but I haven't seen a 
 clear way to cast a char*[] to a char** without it losing the 
 rest of the members. Can anyone help?
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(); }
May 13 2019
parent Doug Clayton <dougclayton0242 gmail.com> writes:
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.
May 13 2019