main 中的 var arg 列表
我想像这样使用我的程序:
./program -I /usr/include/ /usr/bin/ /usr/local/include/ ...
开关可以像在 var args 列表中一样不断地打开。我怎样才能在 C99 中做到这一点?最好得到像 char **args_list
或 char *args_list[]
这样的东西,其中包含 /usr/include
和 等所有内容>/usr/bin/
。
I want to use my program like this:
./program -I /usr/include/ /usr/bin/ /usr/local/include/ ...
Where the switch can go on and on like in a var args list. How could I do that in C99? Preferably get a something like char **args_list
or char *args_list[]
that contains all of the things like /usr/include
and /usr/bin/
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运行以下代码的输出:
由
program -I /usr/include/ /usr/bin/ /usr/local/include/
执行输出:
请注意,在代码示例中,初始索引为 <代码>1。这是因为 argv 变量中的第一个指针是程序的名称。在本例中,它将是
program
。The output of running the following code:
Executed by
program -I /usr/include/ /usr/bin/ /usr/local/include/
Output:
Note that in the code example the initial index is
1
. This is because the first pointer in theargv
variable is the name of the program. In this case it would beprogram
.更新中的程序可能会出现段错误,因为您正在超出数组末尾:
无法保证存在
argv[2]
。(如果 argc==2 它可能为 null,但我认为并非所有 printfs 都能处理这个问题。)
Your program in the update is probably segfaulting because you're running off the end of the array:
there's no guarantee there is an
argv[2]
.(It may be null if argc==2, but I think not all printfs cope with that.)