digitalmars.D - converting a char[][] to a char**
- glen (11/11) Apr 20 2006 Hi All,
- Derek Parnell (45/60) Apr 20 2006 On the assumption that 'char**' points to an array of 'char*' and that
- BCS (11/22) Apr 21 2006 this is quick and dirty but should work
- Daniel Keep (13/17) Apr 21 2006 Read it right-to-left.
Hi All, This seemingly simple thing is causing me alot of trouble. Can someone show me how I can convert an array of char[][] to a char**. Here is the pseudo code... void runWithArgs( char[][] args ) { char** execArgs; // convert args to execArgs execv( toStringz( "myprog" ), execArgs ); } thanks, Glen
Apr 20 2006
On Fri, 21 Apr 2006 02:07:17 +0000 (UTC), glen wrote:Hi All, This seemingly simple thing is causing me alot of trouble. Can someone show me how I can convert an array of char[][] to a char**. Here is the pseudo code... void runWithArgs( char[][] args ) { char** execArgs; // convert args to execArgs execv( toStringz( "myprog" ), execArgs ); }On the assumption that 'char**' points to an array of 'char*' and that array is terminated with a null pointer, then this might be what you are looking for ... ----------------- import std.string; char** toArgList(char[][] pOrig) { char*[] lTemp; lTemp.length = pOrig.length + 1; foreach(int i, char[] pc; pOrig) { lTemp[i] = toStringz(pc); } lTemp[$-1] = null; return lTemp.ptr; } void main(char[][] pArgs) { char** lOldStyle; lOldStyle = toArgList(pArgs); // Use the counted method of accessing them. for(int i = 0; i < pArgs.length; i++) { printf("%d '%s'\n", i, *(lOldStyle+i)); } // Use the terminating null method of accessing them. int j; char* cp; while(true) { cp = *(lOldStyle+j); if (cp == null) break; printf("%d '%s'\n", j, cp); j++; } } ----------------- -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 21/04/2006 1:07:07 PM
Apr 20 2006
In article <e29eol$5sa$1 digitaldaemon.com>, glen says...Hi All, This seemingly simple thing is causing me alot of trouble. Can someone show me how I can convert an array of char[][] to a char**. Here is the pseudo code... void runWithArgs( char[][] args ) { char** execArgs; // convert args to execArgs execv( toStringz( "myprog" ), execArgs ); } thanks, Glenthis is quick and dirty but should work char[][] src; // what to convert alias char* cahr_p; // alias b/c I can't remember the syntax for a array of ptrs char_p[] res = new char_p[src.length+1]; foreach(int i, char[] st, src) res[i] = st.ptr; res[$-1] = null; // now use res.ptr
Apr 21 2006
[snip]alias char* cahr_p; // alias b/c I can't remember the syntax for a array of ptrsRead it right-to-left. char*[] <-- array of pointers to chars If that doesn't help, you can always make an alias, and *then* expand it. alias char* char_p; char_p[] --> (char*)[] --> char*[] At least, that's how I work it out :) -- Daniel -- v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Apr 21 2006