argv [0]可以包含一个空字符串吗? [英] Can argv[0] contain an empty string?

查看:103
本文介绍了argv [0]可以包含一个空字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何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]包含在内的应包含指向字符串的指针,该指针已给出 程序启动前由主机环境实现定义的值.这 目的是向程序提供在程序启动之前确定的信息 来自托管环境中的其他位置. [...]

  • If the value of argc is greater than zero, the array members argv[0] through argv[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]永远不会为空,并且始终指向字符串.不过,字符串本身可能为空.如果n为零,则argv[0]为空.)

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屋!

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