#include <stdio.h> main(){ printf("Poczatek\n"); // что за последний аргумент, я читал, что это // какой-то указатель, никак к сожалению понять не могу. execlp("ls", "ls", "-a", NULL); printf("Koniec\n"); } 2 answers
I read that this is some kind of pointer, unfortunately I can’t understand
Let's try to read man execlp together:
int execlp(const char *file, const char *arg, ...); The const char * arg parameter and similar entries in the execl, execlp, and execle functions assume the parameters arg0, arg1, ..., argn. Together, they describe one or more pointers to lines ending in NULL, which are a list of parameters available to an executable program. The first parameter, by convention, must point to the name associated with the file to be executed. The parameter list must end with NULL.
Pay attention to the last sentence. It means that NULL is used to indicate the end of the list of arguments. This is one of the common mechanisms to say "there are no more arguments" when calling functions with a variable number of arguments.
By the way, do you understand that if ls is launched successfully, you will never get here?
printf("Koniec\n"); - I understood everything, except NULL. Thank you - Vladyslav Alifanov
The last argument of the execlp function - NULL is a sign of the end of the list of arguments. The execlp function returns 0 when the new application loads successfully, and on error it returns -1.
- four" The execlp function returns 0 when the new application loads successfully " - by no means. The functions of the
exec*family replace the current process with a new one; therefore, in case of success, the “return” of them cannot happen in principle. - PinkTux