PDA

View Full Version : execlp question



madpig
February 4th, 2010, 12:16 AM
I was wandering, why when I use a execlp function I have to add NULL as last argument?
execlp(arg1, arg2, ..., NULL)
Could anyone help me to understand? I searched all over the web but cannot find anything useful.
THX

denarced
February 4th, 2010, 05:44 AM
That's funny.
I guess the function's just built that way but I can't figure out why.

lisati
February 4th, 2010, 05:47 AM
This is just a guess (someone can correct me if I'm mistaken), but some functions can accept multiple arguments. Having NULL as the last one is a way of telling the called function when to stop checking for arguments.

Brandon Williams
February 4th, 2010, 06:07 AM
The exec family of functions always takes a NULL terminated array of arguments. You can either specify it as a vector (e.g. execvp) or a list (e.g. execlp). In both cases, there is no way to tell the system how many elements there are in the array, so NULL termination is simply a way to mark the end of the array. The NULL serves the same purpose as the '\0' at the end of a C-string. Also, requiring the NULL array termination makes the input to exec look the same as the argv array that the program being executed will receive.

madpig
February 4th, 2010, 08:40 PM
I thought there is some another way to count arguments. Thanks for help.