大家早上好!我正在尝试通过传递的“-e”参数(例如 parent -e child key1=val1 ...)从父程序创建 fork/exec 调用。因此,我想将 argv 数组中前两个值之后的所有值复制到一个新数组 child_argv 中。像这样的东西:
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
这样我就可以通过类似的方式调用 exec 端:
execl(sExecute, child_argv);
但是我收到错误消息“错误:无法将参数‘2’的‘const char**’转换为‘const char*’到‘execl(const char*, const char*, ...)’”。我什至尝试使用中间步骤:
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
但是我想不通。任何帮助将不胜感激!
更新
正如所指出的,在这种情况下我应该使用“execv”而不是“execl”。虽然仍然有错误......
更新 2
我最终复制了没有所需 argv 参数的数组。查看此处的帖子以查看结果 How to copy portions of an array into another array
最佳答案
来自这里:http://linux.die.net/man/3/exec
我想你的意思是叫“execv”而不是“execl”。 Execl 似乎接受可变数量的参数,期望每个 const char * 是另一个参数,而 execv 接受一个参数数组。
关于c++ - 无法将 'const char **' 转换为 'const char*',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32229637/