Arguments to execvp

Hello everyone

I have an example code:

pid = fork();
if (pid == 0) {
   execvp(argv[2],&argv[2]);
   perror("Error");
}else {
wait(NULL);

}  

Of man execI understand that

"The first argument, by convention, must point to the file name associated with the executable."

So, if I executed my program as follows:

./a.out 5 ls

The ls command will be executed.

What about the second argument? the manual says

"The array of pointers must be interrupted by a NULL pointer"

and I don’t see the NULL pointer here, and I don’t understand what is a function here &argv[2].

Many thanks!

+3
source share
2 answers

execvp - char*, argv. execvp , , "" NULL, , {"foo", "bar"} argv, execvp {"foo", "bar", NULL}. , argv, main, NULL, &argv[2] execvp , NULL .

+7

a.out, , , main :

int main(int argc, char *argv[])

/* argv contains this. */
argv[0] == "a.out"
argv[1] == "5"
argv[2] == "ls"
argv[3] == NULL /* Here is your terminator. */

, argv[2] execvp, , 2 ( ls).

+1

All Articles