char * envp []作为main()可移植的第三个参数 [英] Is char *envp[] as a third argument to main() portable

查看:151
本文介绍了char * envp []作为main()可移植的第三个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在 C 程序中获取一个环境变量,可以使用以下内容:

In order to get an environment variable in a C program, one could use the following:


  • getenv()

  • extern char ** environ;

  • getenv()
  • extern char **environ;

但是除了上面提到的,使用 char * envp [] 作为 main()的第三个参数,以将环境变量视为标准的一部分?

But other than the above mentioned, is using char *envp[] as a third argument to main() to get the environment variables considered part of the standard?

#include <stdio.h>

int main(int argc, char *argv[], char *envp[])
{
    while(*envp)
        printf("%s\n",*envp++);
}

char * envp [] 便携式?

推荐答案

功能 getenv 是唯一的由C标准规定。功能 putenv 和外部 environ 是POSIX特定的。

The function getenv is the only one specified by the C standard. The function putenv, and the extern environ are POSIX-specific.

main 参数 envp 未被POSIX指定,但受到广泛支持。

The main parameter envp is not specified by POSIX but is widely supported.


访问环境列表的另一种方法是向main()函数声明
a第三个参数:

An alternative method of accessing the environment list is to declare a third argument to the main() function:

int main(int argc, char *argv[], char *envp[])

然后以与environ相同的方式处理,与
的区别在于它的范围是main()的本地。虽然这个功能是在UNIX系统上广泛实现的
,但是应该避免使用它,因为
除了范围限制外,在SUSv3 中未指定。

This argument can then be treated in the same way as environ, with the difference that its scope is local to main(). Although this feature is widely implemented on UNIX systems, its use should be avoided since, in addition to the scope limitation, it is not specified in SUSv3.

这篇关于char * envp []作为main()可移植的第三个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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