0

I have this code that works (it's a bit stripped down):

char *parmList[6];

parmList[0] ="/root/ssl_send";
parmList[1] ="-m 1";            
...etc...
parmList[5] = NULL;
execvp(parmList[0], parmList);

Now I want to write something to one string in this list with sprintf (it's more correct to say that I want that one pointer of *parmList[6] points to a char array constructed with sprintf). I am getting "Segmentation errors" all the time. I have tried:

  • using malloc,
  • declaring a double array so the memory space is reserved,
  • using snprintf,....

I am obviously doing something wrong. The problem is similliar to Sprintf Segmentation Fault, just that I need to have a list of pointers/char_arrays for execvp.

4

1 回答 1

1

这是使用 sprintf 创建字符串并在参数列表中使用该字符串的代码。确保为 sprintf 输出分配足够的空间。

char *parmList[6];
parmList[0] = "/bin/ls";
char arg1[10];
sprintf(arg1, "%s", "-l");
parmList[1] = arg1;
parmList[2] = NULL;
execvp(parmList[0], parmList);
于 2012-04-19T07:54:48.170 回答