Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I pass argv from main to execv?

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

like image 583
eric.frederich Avatar asked May 16 '12 13:05

eric.frederich


1 Answers

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.
like image 91
unwind Avatar answered Sep 30 '22 03:09

unwind