After I compile using gcc -Wall getopt.c -o options
and run a few examples, it seems to work at first glance. Purposely tripping it up makes it Segfault.
//straight from the man page
static struct option long_options[] = {
{"add", required_argument, 0, 0 },
{"append", no_argument, 0, 0 },
{"delete", required_argument, 0, 0 },
{"verbose", no_argument, 0, 0 },
{"create", required_argument, 0, 'c'},
{"file", required_argument, 0, 0 },
{0, 0, 0, 0 } //<-- if i omit this line, it segfaults
};
Why does it cause a Segmentation Fault when I omit that one line?
or rather, asked in a different way
Why must I initialize the last set of struct option array
members to null?
Easy. So the code processing the array knows when it's at the end. It's called a sentinel.