为什么我们需要的argc而总是有ARGV的最后一个空? [英] Why do we need argc while there is always a null at the end of argv?

查看:307
本文介绍了为什么我们需要的argc而总是有ARGV的最后一个空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来, ARGV [ARGC] 总是 NULL ,所以我认为我们可以遍历参数列表没有 ARGC 。一个,而循环会做到这一点。

It seems that the argv[argc] is always NULL, so I think we can traverse the argument list without argc. A single while loop will do this.

如果始终有一个 NULL 的argv 的结尾,为什么我们需要一个 ARGC

If there is always a NULL at the end of argv, why do we need an argc?

推荐答案

ARGV [ARGC] == NULL 有保证。见的 C11 5.1.2.2.1程序启动的(我的重点)

Yes, argv[argc]==NULL is guaranteed. See C11 5.1.2.2.1 Program startup (my emphasis)

如果它们被声明,参数的主要功能应服从
  以下限制:

If they are declared, the parameters to the main function shall obey the following constraints:

ARGC的值应为正数。
   ARGV [ARGC]应为空
  指针。

The value of argc shall be nonnegative. argv[argc] shall be a null pointer.

提供了 ARGC 因此不是至关重要的,但仍然是有用的。除其他事项外,它可以快速检查正确的参数数量已经过去了。

Providing argc therefore isn't vital but is still useful. Amongst other things, it allows for quick checking that the correct number of arguments has been passed.

编辑:这个问题已经被修正,包括C ++。 n3337草案的 3.6.1主要功能的说:

The question has been amended to include C++. n3337 draft 3.6.1 Main function says

2 ......的argc应的从传递给程序的参数数量
  在该程序运行的环境。 .... ARGC的价值
  应非负。 的argv的值[ARGC]应为0

2 ...argc shall be the number of arguments passed to the program from the environment in which the program is run. .... The value of argc shall be non-negative. The value of argv[argc] shall be 0.

这篇关于为什么我们需要的argc而总是有ARGV的最后一个空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆