[][] to **

Jarrett Billingsley kb3ctd2 at yahoo.com
Thu Mar 23 17:25:32 PST 2006


"Lucas Goss" <lgoss007 at gmail.com> wrote in message 
news:dvvhdt$2qe3$1 at digitaldaemon.com...
> How can I convert char[][] to char**?
> But I'm drawing a blank for how to do that in D...
> int main(char[][] args)
> {
> someFunc(???)
> return 0;
> }
>
> // someFunc(char** val)

Well, it's can't really be directly done.  In D, you can easily convert a 
char[] to a char*, but since a char[][] is an array of _D_ arrays of 
characters, you wouldn't be able to just cast it to a char**.

Instead, you'll have to do it manually:

import std.stdio;
import std.string;

char** stringsToCharz(char[][] arr)
{
 char** s = new char*[arr.length];
 foreach(uint i, char[] string; arr)
  s[i] = toStringz(string);

 return s;
}

void main()
{
 char[][] strings = new char[][2];
 strings[0] = "hi";
 strings[1] = "bye";

 char** C = stringsToCharz(strings);

 for(uint i = 0; i < strings.length; i++)
  printf("%s\n", C[i]);
}

I'm assuming that the function that you want to pass this char** to is a 
C-style function, and as thus, I've used toStringz() in order to make the 
strings null-terminated. 





More information about the Digitalmars-d mailing list