为什么从 getopt_long() 省略这一行会导致段错误?

Why does ommitting this one line from getopt_long() make a segfault?

在我使用gcc -Wall getopt.c -o options和运行编译了几个例子之后,乍一看似乎是可行的。故意将其绊倒会导致段错误。

//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
};

为什么我省略那一行会导致段错误?

或者更确切地说,以不同的方式提问

为什么我必须将最后一组 struct option array 成员初始化为 null?

来自man

The last element of the array has to be filled with zeros.

struct option {
           const char *name;
           int         has_arg;
           int        *flag;
           int         val;
       };

 The meanings of the different fields are:

  name   is the name of the long option.

 has_arg
       is: no_argument (or 0) if the option does not take an
          argument; required_argument (or 1) if the option requires an
          argument; or optional_argument (or 2) if the option takes an
          optional argument.

  flag   specifies how results are returned for a long option.  If flag
          is NULL, then getopt_long() returns val.  (For example, the
          calling program may set val to the equivalent short option
          character.)  Otherwise, getopt_long() returns 0, and flag
          points to a variable which is set to val if the option is
          found, but left unchanged if the option is not found.

  val    is the value to return, or to load into the variable pointed
          to by flag.

  The last element of the array has to be filled with zeros.

  If longindex is not NULL, it points to a variable which is set to the
   index of the long option relative to longopts.

简单。所以处理数组的代码知道它何时结束。它被称为哨兵。