Suppose I want to forward a set of command line options to a C function, declared as follows, with the D main taking args
extern (C) void init(int argc, char** argv);
void main(string[] args) {
init(args.length, map!(toStringz)(args));
}
The first parameter is easy enough, but my attempt at applying toStringz
to the args
array doesn't seem to work. I get cannot implicitly convert expression (map(args)) of type MapResult!(toStringz,string[]) to char**
. How do you convert string[]
to char**
(or even const(char)**
).
std.algorithm.map
function returns range, which in your case must be changed into array. You can do this using std.array.array
. Then you can get array pointer using .ptr
. It returns immutable(char**)
which is casted into 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 live demo: http://dpaste.dzfl.pl/ff81984c