linux-sysprog · beginner · ~15 min
Walk a NULL-terminated pointer array.
The exec family receives arguments as a NULL-terminated array of string pointers (argv). Count how many arguments it holds.
Implement int argv_len(const char *const *argv) that returns the number of entries before the terminating NULL pointer.
argv: array of char *, terminated by a NULL pointer.The count of non-NULL entries.
argv_len({"ls", "-l", "/tmp", NULL}) -> 3
argv_len({NULL}) -> 0
argv: NULL-terminated array of string pointers.
Number of entries before the NULL terminator.
The array is always NULL-terminated.
#include <stddef.h>
int argv_len(const char *const *argv) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.