converting a char[][] to a char**
    Derek Parnell 
    derek at psych.ward
       
    Thu Apr 20 20:14:42 PDT 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
    
    
More information about the Digitalmars-d
mailing list