execl() arguments in Ubuntu

后端 未结 4 1713
慢半拍i
慢半拍i 2021-01-12 07:42

I am learning linux programming and came across exec function which is kind of very useful. But the problem is exec function arguments are very confusing and I am unable to

4条回答
  •  旧时难觅i
    2021-01-12 07:50

    The exec functions are variadic: they take a variable number of parameters so that you can pass a variable number of arguments to the command. The functions need to use NULL as a marker to mark the end of the argument list.

    Within variadic functions is a loop that will iterate over the variable number of arguments. These loops need a terminating condition. In some cases, e.g. printf, the actual number of arguments can be inferred from another argument. In other functions, NULL is used to mark the end of the list.

    Another option would be to add an additional function parameter for number of arguments, but that would make the code a little more brittle, requiring the programmer to manage an additional parameter, rather than simply always using a NULL as the final argument.

    You'll also see (char *) 0 used as the marker:

    execl("/bin/ls", "ls", "-l", (char *) 0);
    

提交回复
热议问题