argv[0] 可以包含空字符串吗? [英] Can argv[0] contain an empty string?
问题描述
在任何 C 程序中,命令行参数 argv[0]
指向用于调用程序的名称.是否有任何情况下它会指向一个空字符串 ""
?
In any C program, the command line argument argv[0]
points to the name used to invoke the program. Is there any circumstance in which it will point to an empty string ""
?
这种情况的示例代码片段将是一个很好的参考.
An example code snippet for such a case would be a good reference.
推荐答案
它是实现定义的.§5.1.2.2.1 删节:
It's implementation defined. §5.1.2.2.1 abridged:
如果
argc
的值大于零,数组成员argv[0]
到argv[argc-1]
inclusive 应包含指向字符串的指针在程序启动之前由主机环境实现定义的值.这意图是向程序提供在程序启动之前确定的信息来自托管环境中的其他地方.[...]
If the value of
argc
is greater than zero, the array membersargv[0]
throughargv[argc-1]
inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. [...]
如果argc
的值大于零,则为argv[0]
指向的字符串表示程序名称;argv[0][0]
应为空字符,如果程序名称在宿主环境中不可用.[...]
If the value of argc
is greater than zero, the string pointed to by argv[0]
represents the program name; argv[0][0]
shall be the null character if the
program name is not available from the host environment. [...]
所以如果 argc
大于零,意图 argv[0]
永远不会是一个空字符串,但它可以发生.(注意 argc
等于 n
,argv[0]
到 argv[n - 1]
永远不会null 并且总是指向一个字符串.不过,字符串本身可能是空的.如果 n
为零,则 argv[0]
为 null.)
So if argc
is greater than zero, it's quite the intention that argv[0]
never be an empty string, but it could happen. (Note that with argc
equal to n
, argv[0]
through argv[n - 1]
are never null and always point to a string. The string itself may be empty, though. If n
is zero, argv[0]
is null.)
当然,在实践中,您只需要确保您的目标平台按需要运行即可.
In practice, of course, you just need to make sure the platforms your targetting behave as needed.
这篇关于argv[0] 可以包含空字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!