c++ - 无法将 'const char **' 转换为 'const char*'

标签 c++ arrays char

大家早上好!我正在尝试通过传递的“-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/

相关文章:

c++ - 这是 g++ bug、libc bug,还是我疯了(或者也许这三个)?

c++ - 替换 dll 驱动程序及其完整性

c++ - 如何在 Linux 上从源代码安装 TBB 并使其工作

c++ - 如何识别 C++ 中的新行?

arrays - 将数组添加到数组而不创建二维数组

java - 练习使用二进制搜索将数据插入数组,问题很少

c - 在 C 中打印出一个没有预定长度的数组

c - 使用 C 返回数组

c# - 将 char 隐式转换为整数有什么好处?

java - 使用 JDBC 时密码是字符串还是 char[]?