首页 > ”argv的第一个元素指向一个空字符串“是什么意思?

”argv的第一个元素指向一个空字符串“是什么意思?

当实参传给main函数之后,argv的第一个元素指向程序的名字或者一个空字符串

前半句话我知道,主要是后半句话,argv[0]='\0',这算啥?


argv[0] is the pointer to the initial character of a null-terminated multibyte strings that represents the name used to invoke the program itself (or, if this is not supported by the execution environment, argv[0][0] is guaranteed to be zero).

http://en.cppreference.com/w/c/language/main_function

argv[0] is the pointer to the initial character of a null-terminated multibyte strings that represents the name used to invoke the program itself (or an empty string "" if this is not supported by the execution environment).

http://en.cppreference.com/w/cpp/language/main_function


首先你得知道进程的创建。
以 Windows 平台为例,标准是通过 CreateProcess 创建
CreateProcess MSDN: https://msdn.microsoft.com/zh-cn/library/ms682425(v=vs.85).aspx

BOOL WINAPI CreateProcess(
  _In_opt_    LPCTSTR               lpApplicationName,
  _Inout_opt_ LPTSTR                lpCommandLine,
  _In_opt_    LPSECURITY_ATTRIBUTES lpProcessAttributes,
  _In_opt_    LPSECURITY_ATTRIBUTES lpThreadAttributes,
  _In_        BOOL                  bInheritHandles,
  _In_        DWORD                 dwCreationFlags,
  _In_opt_    LPVOID                lpEnvironment,
  _In_opt_    LPCTSTR               lpCurrentDirectory,
  _In_        LPSTARTUPINFO         lpStartupInfo,
  _Out_       LPPROCESS_INFORMATION lpProcessInformation
);

第一个参数是程序的绝对路径,第二个是命令行参数,如果第一个参数存在,而第二个为空,那么就出现 argv 为空了。

而 Linux 下,内部实现也主要是

int execle(const char *path, const char *arg , ..., char * const envp[]); 

也就是 arg 为空了。
在 Windows 中,打开任务管理器,选取显示命令行参数,有很多系统进程是没有命令行参数的。

大多数情况下,至少有一个命令行参数是它本身。

【热门文章】
【热门文章】