getopt_long 将 optstring[0] 设置为 '+'

问题描述 投票:0回答:1

根据 getopt 和 getopt_long 的手册页,GNU 版本对 argv 进行了重新排序,以便任何类似于标志的东西都排在第一位,然后当它到达第一个不是标志的字符串时返回 -1。它还表示,当 optstring[0] 设置为“+”时,它不会对参数重新排序。如何将 optstring[0] 设置为“+”?我试着简单地扔了一个 optstring[0] = '+';赋值语句,我理所当然地知道 optstring 是未声明的。

c getopt
1个回答
2
投票

optstring
getopt_long
的第三个参数,声明为:

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

使用以

optstring
开头的
+
调用函数:

getopt_long(argc, argv, "+abc:d:f:", long_options, &option_index);
© www.soinside.com 2019 - 2024. All rights reserved.