The execv() function expects an array of NULL terminated strings but doesn't take the number of arguments. It uses a sentinel value (NULL pointer) to determine when the array ends.
The man page for execv() states...
The first argument, by convention, should point to the filename associated with the file being executed. The array of pointers must be terminated by a NULL pointer.
... so my question is....
I want to pass the argv from main() to execv().
Can I be assured that argv that comes into main is terminated by a NULL pointer?
That is, can I be assured that argv[argc] == NULL or do I have to allocate my own char* array of size argc + 1 and put NULL in the argc index?
If I can be assured, is it documented somewhere?
Thanks, ~Eric
Yes. The argv vector is always NULL terminated.
The draft C99 standard states:
If they are declared, the parameters to the
mainfunction shall obey the following constraints:
- The value of
argcshall be nonnegative.argv[argc]shall be a null pointer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With