www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [][] to **

reply Lucas Goss <lgoss007 gmail.com> writes:
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)
Mar 23 2006
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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.
Mar 23 2006
parent Lucas Goss <lgoss007 gmail.com> writes:
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
Mar 23 2006