www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - char ***argc problems.

reply "Andrew" <andrew.spott gmail.com> writes:
I'm attempting to create a wrapper for MPI, however, MPI_Init
wants to read the arguments for main():

MPI_Init(int *argv, char ***argc);

How do I get this last level of pointer reference?

So far, I have:

void main (string[] args)
{
       auto argarr = new char*[args.length];
       foreach(i, a; args)
           argarr[i] = (a.dup ~ '\0').ptr;

       int argc = to!(int)(argarr.length);
       MPI_Init(&argc, argarr.ptr);
}

Any ideas?

-Andrew
Aug 12 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 08/12/2012 10:33 PM, Andrew wrote:
 I'm attempting to create a wrapper for MPI, however, MPI_Init
 wants to read the arguments for main():

 MPI_Init(int *argv, char ***argc);

 How do I get this last level of pointer reference?

 So far, I have:

 void main (string[] args)
 {
     auto argarr = new char*[args.length];
     foreach(i, a; args)
     argarr[i] = (a.dup ~ '\0').ptr;

     int argc = to!(int)(argarr.length);
     MPI_Init(&argc, argarr.ptr);
 }

 Any ideas?

 -Andrew
Maybe like this? import std.algorithm, std.array, std.conv; void main(string[] args){ auto argarr = args.map!(a=>(a ~ '\0').ptr).array; int argc = to!(int)(argarr.length); auto argv = argarr.ptr; MPI_Init(&argc, &argv); args = argv[0..argc].map!(to!string).array; // ... }
Aug 12 2012
parent reply "Andrew" <andrew.spott gmail.com> writes:
On Sunday, 12 August 2012 at 20:57:34 UTC, Timon Gehr wrote:
 On 08/12/2012 10:33 PM, Andrew wrote:
 I'm attempting to create a wrapper for MPI, however, MPI_Init
 wants to read the arguments for main():

 MPI_Init(int *argv, char ***argc);

 How do I get this last level of pointer reference?

 So far, I have:

 void main (string[] args)
 {
    auto argarr = new char*[args.length];
    foreach(i, a; args)
    argarr[i] = (a.dup ~ '\0').ptr;

    int argc = to!(int)(argarr.length);
    MPI_Init(&argc, argarr.ptr);
 }

 Any ideas?

 -Andrew
Maybe like this? import std.algorithm, std.array, std.conv; void main(string[] args){ auto argarr = args.map!(a=>(a ~ '\0').ptr).array; int argc = to!(int)(argarr.length); auto argv = argarr.ptr; MPI_Init(&argc, &argv); args = argv[0..argc].map!(to!string).array; // ... }
I don't know why I didn't think of this: auto argv = argarr.ptr; Which fixed it. Unfortunately, using args.map! doesn't work, because it returns an immutable, which MPI_Init can't take. Thanks for the help -Andrew
Aug 12 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 08/12/2012 11:38 PM, Andrew wrote:
 On Sunday, 12 August 2012 at 20:57:34 UTC, Timon Gehr wrote:
 On 08/12/2012 10:33 PM, Andrew wrote:
 I'm attempting to create a wrapper for MPI, however, MPI_Init
 wants to read the arguments for main():

 MPI_Init(int *argv, char ***argc);

 How do I get this last level of pointer reference?

 So far, I have:

 void main (string[] args)
 {
    auto argarr = new char*[args.length];
    foreach(i, a; args)
    argarr[i] = (a.dup ~ '\0').ptr;

    int argc = to!(int)(argarr.length);
    MPI_Init(&argc, argarr.ptr);
 }

 Any ideas?

 -Andrew
Maybe like this? import std.algorithm, std.array, std.conv; void main(string[] args){ auto argarr = args.map!(a=>(a ~ '\0').ptr).array; int argc = to!(int)(argarr.length); auto argv = argarr.ptr; MPI_Init(&argc, &argv); args = argv[0..argc].map!(to!string).array; // ... }
I don't know why I didn't think of this: auto argv = argarr.ptr; Which fixed it. Unfortunately, using args.map! doesn't work, because it returns an immutable, which MPI_Init can't take.
oops. auto argarr = args.map!(a=>(a.dup ~ '\0').ptr).array;
 Thanks for the help

 -Andrew
Aug 12 2012