www.digitalmars.com         C & C++   DMDScript  

D - Q: char[][] args to C char**argv

reply Ant <Ant_member pathlink.com> writes:
How do we do that?

D source:

extern(C) c_parse_args((int argc,char **argv);

void main(char[][] args)
{
     char** argv = ?;

     int argc = args.length

     c_parse_args(argc, argv);
}

Ant
Sep 27 2003
next sibling parent reply "Walter" <walter digitalmars.com> writes:
char** argv = new char*[args.length];
int i = 0;
foreach (char[] p; args)
{
    argv[i++] = cast(char*)p;
}

"Ant" <Ant_member pathlink.com> wrote in message
news:bl5s9q$140q$1 digitaldaemon.com...
 How do we do that?

 D source:

 extern(C) c_parse_args((int argc,char **argv);

 void main(char[][] args)
 {
      char** argv = ?;

      int argc = args.length

      c_parse_args(argc, argv);
 }

 Ant

Sep 28 2003
parent Ant <Ant_member pathlink.com> writes:
In article <bl62sm$1dq0$1 digitaldaemon.com>, Walter says...
char** argv = new char*[args.length];
int i = 0;
foreach (char[] p; args)
{
    argv[i++] = cast(char*)p;
}

Thanks. Maybe this couldl be on one of the C to D pages. Thanks also to Helmut. Ant
Sep 28 2003
prev sibling parent Helmut Leitner <helmut.leitner chello.at> writes:
Ant wrote:
 
 How do we do that?
 
 D source:
 
 extern(C) c_parse_args((int argc,char **argv);
 
 void main(char[][] args)
 {
      char** argv = ?;
 
      int argc = args.length
 
      c_parse_args(argc, argv);
 }

This should do the job: import string; alias char [] String; char** ArgsRetArgv(String [] args) { char* [] argv; argv.length=args.length+1; for(int i; i<args.length; i++) { argv[i] = (char *)args[i]; } return argv; } int main (String [] args) { char** argv=ArgsRetArgv(args); // c_parse_args(args.length,argv); return 0; } Its based on the (tested) assumption, that args consists of slices on the existing '\0'-terminated argv-buffer. -- Helmut Leitner leitner hls.via.at Graz, Austria www.hls-software.com
Sep 28 2003