Question

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

Was it helpful?

Solution

Yes. The argv vector is always NULL terminated.

The draft C99 standard states:

If they are declared, the parameters to the main function shall obey the following constraints:

  • The value of argc shall be nonnegative.
  • argv[argc] shall be a null pointer.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top