Passing a zero as an argument

Let's say we have a (fictional) requirement: From within our C code, we must execute the program /projectx/do_this_now passing along three parameters: -1, 0 and 55. Like so:

/projectx/do_this_now -1 0 55

Recall the syntax of the exec API:

execl(const char *pathname_to_successor_program, const char *argv0, const char *argv1, ..., const char *argvn, (char *)0);

So, it seems quite trivial; let's do it:

execl("/projectx/do_this_now", "do_this_now", -1, 0, 55, (char *)0);

Whoops! The compiler will, or could, interpret the second argument to the successor 0 (after the -1) as the NULL terminator, and would therefore not see the following argument 55.

Fixing this is easy; we just have to remember that each argument to ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.