Why does the struct options array require additional dummy entries when using getopt_long

For example, an array of parameters:

static struct option const long_options[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; 

Is it for filling?

+4
source share
2 answers

See the getopt_long() man page:

 int getopt_long(int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longindex); 

A pair of argc and argv shows one way to tell how many records are in the array (by and large, although with argv[argc] == 0 there is also a watch there). optstring indicates short arguments; longindex is the output parameter. This leaves only the longopts pointer, which means that the function should be able to determine how many records in the array do not support counting (there is no longoptcount argument), so the end of the array is marked with all zero values ​​- a sentinel value.

+2
source

This is a "watch", so the code that processes the array knows when it has reached the end.

+3
source

Source: https://habr.com/ru/post/1332403/


All Articles