使用 exec
族函数时抛出以下警告:
1 2 3 4 |
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 中找到:
1 |
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
。
评论