user1646428
user1646428

Reputation: 179

cannot convert 'const char **' to 'const char*'

Good morning everyone! I am trying to create a fork/exec call from a parent program via a passed '-e' parameter (e.g. parent -e child key1=val1 ...). As a result I want to copy all the values after the first two in the argv array into a new array child_argv. Something like:

const char *child_argv[10];  // this is actually a global variable
static const char *sExecute;

      int I = 0;
const char *Value = argv[1];
    sExecute = Value;

      for (i=2; i<argc; i++) {
             child_argv[I] = argv[i];
             I++;
      }
    child_argv[I] = NULL;   // terminate the last array index with NULL

This way I can call the exec side via something like:

execl(sExecute, child_argv);

However I get the error message "error: cannot convert 'const char**' to 'const char*' for argument '2' to 'execl(const char*, const char*, ...)'". I have even tried to use an intermediate step:

const char *child_argv[10];  // this is actually a global variable
static const char *sExecute;

      int I = 0;
const char *Value = argv[1];
    sExecute = Value;

      for (i=2; i<argc; i++) {
    const char *Value = argv[i+1];
             child_argv[I] = Value;
             I++;
      }
    child_argv[I] = NULL;   // terminate the last array index with NULL

But I can't figure this out. Any help would be greatly appreciated!

UPDATE

As was pointed out, I should be using 'execv' instead of 'execl' in this situation. Still getting errors though...

UPDATE 2

I ended up copying the array without the desired parameters of argv. See the post here to see the result How to copy portions of an array into another array

Upvotes: 0

Views: 3528

Answers (2)

pinkhorror
pinkhorror

Reputation: 1

You should be using execv. When you convert to execv, for some reason execv expects a array of non-const pointers instead of const pointers, so you either have to just cast the array to (char**) or copy the strings into (char*) pointers.

Upvotes: 0

Chris Beck
Chris Beck

Reputation: 16204

From here: http://linux.die.net/man/3/exec

I think you mean to call "execv" not "execl". Execl seems to take a variable number of arguments, expecting each const char * to be another argument, while execv takes an array of arguments.

Upvotes: 2

Related Questions