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

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

问题描述

为了在 C 程序中获取环境变量,可以使用以下代码:

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

  • 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
",*envp++);
}

char *envp[] 可移植吗?

推荐答案

函数getenv是C标准唯一指定的.函数 putenv 和 extern 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() 函数的第三个参数:

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天全站免登陆