envp 代表什么?

What does envp stand for?

我想知道缩写envp代表什么,例如这里:

int main(int argc, char **argv, char **envp);

我也想知道 argv 中的 v 最初代表什么。 v 是“值”吗?或者也许是“向量”?

意思是:

  • argv代表argument vector
  • argc for argument count, and
  • envp for environment pointer

我们可以讨论命名约定的好坏,但它是一个历史性的用法,可以追溯到 C 语言的开始:B.W.Kernighan and D.Ritchie used already argc and argv for main() in their first edition of The C Programming language 1978 年。

envp was added later from the UNIX development and is an alternative for using the environ pointer. I found a reference to it in a book from 1986, but it's certainly even older. It doesn't need a count, because it's null terminated. Note that it is not portable and therefore the use of getenv() 应该是首选。

来自 C 语言规范:

J.5.1 Environment arguments
1 In a hosted environment, the main function receives a third argument, char *envp[], that points to a null-terminated array of pointers to char, each of which points to a string that provides information about the environment for this execution of the program (5.1.2.2.1).

请注意,此第三个参数未在 C++ 语言规范中列出,但某些实现仍然适用。

我没有研究过常规参数名称的词源,所以请原谅我的推测性回答。

'env' 很明显是指环境或环境变量。

envp 中的 'p' 可能是参数的初始值,或者可能是指向类型的指针。甚至 P 程序的环境变量。

词源对于程序的意义来说并不是很重要,所以我建议不要太担心它。

在 Kernighan 和 Ritchie 的著作中,The C Programming Language,1978 年,argv 在第 221 页的索引中显示为:

argv argument vector 110

第 110-114 页的文字除了语义之外,没有进一步说明 argcargv 的来源。 envp 没有出现在索引中。

1988 年第 2 版第 114 页相应部分的文字说:

… two arguments. The first (conventionally called argc, for argument count) is the number of command-line arguments the program was invoked with; the second (argv, for argument vector) is…

同样在第二版中,envp没有出现在索引中。

Unix 规范,IEEE Std 1003.1-2008,2016,The Open Group Base Specifications Issue 7,在 environ、[=19= 的部分中说],以及相关例程:

Some implementations provide a third argument to main() called envp. This is defined as a pointer to the environment.

这与 envp 代表 environment pointer.

一致