【发布时间】:2015-08-26 14:39:45
【问题描述】:
大家早上好!我正在尝试通过传递的“-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
【问题讨论】:
-
您的代码不包含
execl调用,这是编译错误的来源。但就像错误所暗示的那样,当它需要const char*时,您正在传递child_argv(const char**类型)。 -
我推荐你阅读the
execlmanual page,错误的原因应该很明显了。实际上,如果您阅读错误信息,您也会明白其中的原因。 -
@Barry 'execl' 调用已包含在帖子中。当 child_argv 创建为 const char* 数组时,我如何传递 const char**?