Convert string[] to char**

Jacob Carlborg doob at me.com
Tue Jun 4 01:45:19 PDT 2013


On 2013-06-04 09:18, Bruce Smith wrote:
> Hello
>
> /[I'll prefix this question by stating that I am not a strong C
> programmer; my long experience is in a wide range of languages other
> than C//]/
>
> I wish to call some functions in a complex C library from D. Since this
> a large & complex library, I've wrapped a bunch of the functions using
> SWIG (swig -d -d2 ...). One of the C functions has a signature like:
>
> void foo(const char * const *keys);
>
> SWIG has given me a D function signature like:
>
>      void foo(char** keys);
>
> In my D program it is natural to represent some keys as an array of
> strings, for example:
>
>      string[] mykeys;
>
> How should I convert mykeys from string[] to char** so that I can call
> foo with mykeys as the actual parameter?

I think you need to do something like:

import std.conv;

string[] mykeys;
char*[] ckeys;
ckeys.reserve(mykeys.length);

foreach (key ; mykeys)
     ckeys ~= to!(char*)(key);

foo(ckeys.ptr);

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list