Convert string [] to char **

Suppose I want to redirect a set of command-line options to a C function declared as follows using D main accepting args

extern (C) void init(int argc, char** argv); void main(string[] args) { init(args.length, map!(toStringz)(args)); } 

The first parameter is quite simple, but my attempt to apply toStringz to the args array does not work. I get cannot implicitly convert expression (map(args)) of type MapResult!(toStringz,string[]) to char** . How to convert string[] to char** (or even const(char)** ).

+4
source share
1 answer

std.algorithm.map function returns a range, which in your case should be changed to an array. You can do this using std.array.array . Then you can get a pointer to the array using .ptr . It returns immutable(char**) , which is placed in char** :

 extern (C) void init(int argc, char** argv); void main(string[] args) { init(cast(int)args.length, cast(char**)map!(toStringz)(args).array.ptr); } 

Here's a live demo: http://dpaste.dzfl.pl/ff81984c

+4
source

Source: https://habr.com/ru/post/1442799/


All Articles