Tuesday, July 28, 2009

Help passing pointers as arguments for execl() system call in C ???

The prototype of the execl() system call is





execl(char *path, char *arg0,...,char *argn, 0);





Say I want to run the ls command. So I enter this statement





execl("/bin/ls","ls", "-l",0);





Now my problem is how "/bin/ls" becomes a char pointer because in function prototype it says I should give a char pointer as my first argument?...





Please help me.. I have a basic knowledge about pointers but I'm really stuck here..

Help passing pointers as arguments for execl() system call in C ???
In C/C++ all arrays are passed by pointers already. Since strings such as "/bin/ls","ls", "-l" are actually character arrays, then they are passed as pointers.





The execl function (and all functions with char *) see the arguments as pointers to character arrays.





ANSI compilers use the syntax (const char* arg) to enforce that the called function cannot alter the contents of array pointed to. Things such as *arg = 'X' are invalid in the called function because the first argument is a (const char *). The (const char *) ensures to the caller that the array which the argument points to will not be modified.





the ANSI prototype for execl is:


int execl(const char *path, const char *arg0,..., const char *argn, 0)





Within the execl funcion, things that try to alter the arguments will result in a compile error.





BTW:


(char const* arg) means that the pointer to the array cannot itself , be altered. Things such as arg++ are invalid in the called function.

survey for money

No comments:

Post a Comment