1

For example, if i have

int main()
{
int status;
pid_t pid;
char* arg[]= {"ls", "-l", "/usr/include",(char *)0};
if ((pid=fork())==0)
{
/* CODICE ESEGUITO DAL FIGLIO */
execv("/bin/ls", arg);
/* Si torna solo in caso di errore */
exit

Why in the line char* arg[]= {"ls", "-l", "/usr/include",(char *)0};

This is the element *(char ) 0}?

What is it?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Matteo
  • 129
  • 2
  • 10
  • 7
    How about taking a look at [this SO post](http://stackoverflow.com/questions/6072931/what-does-char-0-mean-in-c)? – user3078414 May 08 '16 at 12:01
  • You may have a look at this [set](http://stackoverflow.com/search?q=[c]+%22%28char*%29+0%22) of questions. Also try searching SO using advanced options before posting questions. I am sure it will be helpful for you :D – sjsam May 08 '16 at 12:07
  • not sure whether or not this should be dupe: the `exec` family of functions has slightly different requirements to `fgets` – M.M May 08 '16 at 14:20

3 Answers3

3

It's NULL pointer. It serves the same purpose in the array as null terminator character '\0' serves in C strings - provides a way to determine the last valid position in a collection without passing its length explicitly.

Essentially, execv needs to know how many parameters you want to pass. C does not provide a way to find the length of an array passed to a function, so two common patterns are passing the length in a separate parameter and using a marker element to terminate the sequence. For arrays of pointers, NULL is a natural marker element.

Another common way to write the same expression is

char* arg[]= {"ls", "-l", "/usr/include", NULL};
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

(char *)0 is equivalent to NULL pointer

granmirupa
  • 2,780
  • 16
  • 27
  • C does not define a null pointer constant as a pointer to the address 0. – 2501 May 08 '16 at 12:08
  • @2501 No, but (char *)0 is equivalent to write NULL in that context – granmirupa May 08 '16 at 12:18
  • That is true, but that is also not the topic comment, which you didn't address. – 2501 May 08 '16 at 12:24
  • @2501 please explain better. – granmirupa May 08 '16 at 12:26
  • Your answer is plain wrong. `0` when used in pointer context is a _null pointer constant_. That is not necessarily identical to address `0x0`. And a _null pointer_ points **nowhere** - that's exactly what it is for. Also `NULL` is a macro, not a pointer. And no, it is not equivalent. See the standard. – too honest for this site May 08 '16 at 13:57
0

The purpose of this is so that the execv function knows when to stop reading arguments.

After all, by passing arg you only pass a pointer to the first argument. The execv function will find all the arguments by incrementing this to find the next argument, until it finds a special sentinel value. According to the documentation of execv:

The array of pointers must be terminated by a NULL pointer.

(char *)0 is a null pointer of course. So why did the person write (char *)0 instead of NULL?

They may have mixed up execv with execl which does require that specifically (char *)0 or equivalently (char *)NULL be passed.

This is because execl reads arguments via the varargs mechanism , so the actual argument must be specifically a char *. Passing NULL risks that NULL is a macro defined to the constant 0 of type int, which would break the varargs mechanism.

M.M
  • 138,810
  • 21
  • 208
  • 365