Thread overview
[][] to **
Mar 24, 2006
Lucas Goss
Mar 24, 2006
Lucas Goss
March 24, 2006
How can I convert char[][] to char**?

In C I can just do:
int main(int argc, char** argv)
{
	someFunc(args);
	return 0;
}

But I'm drawing a blank for how to do that in D...
int main(char[][] args)
{
	someFunc(???)
	return 0;
}

// someFunc(char** val)
March 24, 2006
"Lucas Goss" <lgoss007@gmail.com> wrote in message news:dvvhdt$2qe3$1@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.


March 24, 2006
Jarrett Billingsley wrote:
> 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:

Ah. Thanks! :D