使用exec族函数时抛出以下警告:

exec.c: In function ‘main’:
exec.c:8:3: warning: missing sentinel in function call [-Wformat=]
   if (execlp("/bin/ls", "/bin/ls", "-l", ".") == -1)
   ^

错误的原因在man page中找到:

The execv(), execvp(), and execvpe() functions provide an array of pointers to null-terminated strings that represent the argument list available to the new program.  The  first  argu‐ment, by convention, should point to the filename associated with the file being executed.  The array of pointers must be terminated by a null pointer.

execv, execvp, execvpe这几个函数可以给新创建的进程传递参数,但是The array of pointers must be terminated by a null pointer这一句告诉我们最后一个参数一定是一个指向NULL的指针。

而从警告内容来看,我的函数调用为:execlp("/bin/ls", "/bin/ls", "-l", "."),最后并没有按照约定传递NULL参数导致出现错误。所以解决方法就是在函数最后加上NULL或者(char*)0

最后修改:2018 年 05 月 21 日
如果觉得我的文章对你有用,请随意赞赏