Why is argv parameter to execvp not const?

不问归期 提交于 2019-12-03 01:15:50

The POSIX spec says (http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html):

The argv[] and envp[] arrays of pointers and the strings to which those arrays point shall not be modified by a call to one of the exec functions, except as a consequence of replacing the process image.

I think the missing (or misplaced) const is simply an historical oddity.

I came across this same situation. Because execvp() has a char *const as the second parameter, that means it accepts a constant pointer to a char. Therefore, if you pass it a pointer char it will be able to cast the pointer char to a constant pointer to a char. So, instead of declaring it

const char* argv[] = {"/bin/my", "command", "here", NULL};

try

char* argv[] = {"/bin/my", "command", "here", NULL};

and it will accept argv[] without issue.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!