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
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);