什么时候可以ARGV [0]为空? [英] When can argv[0] have null?

查看:138
本文介绍了什么时候可以ARGV [0]为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所了解有关参数传递给main()的命令行是ARGC为1和argv最小值[0]总会有程序名与其在它的路径。

What I have understand about passing arguments to main() from command line is that argc has a minimum value of 1 and argv[0] will always have the program name with its path in it.

如果在命令行提供参数,则ARGC将有一个价值超过一和argv 1时以ARGV [ARGC-1]将这些参数。

If arguments are provided at the command line, then argc will have a value greater than one and argv1 to argv[argc-1] will have those arguments.

现在在此链接说,

的argv [0]将包含该程序的名称或一个空字符串如果这是不可用的字符串。

argv[0] will be a string containing the program's name or a null string if that is not available.

现在,如何以及何时能的argv [0]有空字符串?我指的是它的路径永远是可用的程序名称,这样它的时候可以为空?

Now, how and when can argv[0] have null string? I mean program name with its path will always be available so when can it be null?

作家说:如果这是不可用,但是当它是如何可能的程序名称将不可用?

Writer says that "if that is not available" but when and how it is possible that program name will not be available?

感谢您的时间和支持。

问候

推荐答案

随着 EXEC 类的电话,您可以指定程序的名称的和程序的执行的独立,因此您可以将其设置为null,则

With the exec class of calls, you specify the program name and program executable separately so you can set it to NULL then.

但是,报价实际上是从ISO标准(可能是转述)和标准涵盖了从最小的微控制器,以最新的Z10企业级大型机一个非常大范围的执行环境。

But that quote is actually from the ISO standard (possibly paraphrased) and that standard covers a awfully large range of execution environments from the smallest micro-controller to the latest z10 Enterprise-class mainframe.

其中许多嵌入式系统将是在一个可执行文件的名称是没有意义的情况。

Many of those embedded systems would be in the situation where an executable name makes little sense.

从最新的C1X草案:

ARGC 的值应为正数。

ARGV [ARGC] 应是一个空指针。

如果的 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 start up.

这意味着,如果 ARGC 为零(它可以),argv的[0]为NULL。

This means that, if argc is zero (and it can be), argv[0] is NULL.

不过,即使 ARGC 是的的0,你可能不会得到程序名称,因为标准还规定:

But, even when argc is not 0, you may not get the program name, since the standard also states:

如果的 ARGC 的值大于零,则指向的字符串的argv [0] 重新presents程序名称; 的argv [0] [0] 应为空字符,如果程序名称不能从主机环境。如果 ARGC 的值大于1时,琴弦由指向的argv [1] 的argv [ARGC-1] 重新present程序参数。

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. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

因此​​,存在程序名称被设置在标准下没有要求。我见过的程序使用的选项有多种选择此值:

So, there is no requirement under the standard that a program name be provided. I've seen programs use a wide selection of options for this value:


  • 在所有(假定为安全)没有价值。

  • 一个弥天大谎(如睡眠恶意件code的)。

  • 实际的程序名(例如睡眠)。

  • 略加修改一(如 -ksh 为登录shell)。

  • 的描述性名称(例如,的progname - 对某事物的节目)。

  • no value at all (for supposed security).
  • a blatant lie (such as sleep for a malicious piece of code).
  • the actual program name (such as sleep).
  • a slightly modified one (such as -ksh for the login shell).
  • a descriptive name (e.g., progname - a program for something).

这篇关于什么时候可以ARGV [0]为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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