在我使用gcc -Wall getopt.c -o options
并运行了一些示例进行编译之后,乍一看似乎可以正常工作。故意将其绊倒会导致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
};
当我省略那一行时,为什么会引起分段错误?
或者,以不同的方式问
为什么必须将最后一组struct option array
成员初始化为null?
简单。因此,处理数组的代码知道何时结束。这叫做哨兵。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句