Thread overview | ||||||
---|---|---|---|---|---|---|
|
April 21, 2006 converting a char[][] to a char** | ||||
---|---|---|---|---|
| ||||
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 |
April 21, 2006 Re: converting a char[][] to a char** | ||||
---|---|---|---|---|
| ||||
Posted in reply to glen | 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 |
April 21, 2006 Re: converting a char[][] to a char** | ||||
---|---|---|---|---|
| ||||
Posted in reply to glen | 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, >Glen > > this 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 |
April 21, 2006 Re: converting a char[][] to a char** | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | [snip] > > alias char* cahr_p; // alias b/c I can't remember the syntax for a array of ptrs > Read 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/ |
Copyright © 1999-2021 by the D Language Foundation