关于main函数的参数

来源:互联网 发布:淘宝网春英服饰 编辑:程序博客网 时间:2024/05/22 19:40

我晕了。以前一直以为main函数只有两个入口参数:参数个数和参数字符串列表。今天才发现它居然还有第三个参数:环境变量。

说明如下:


对于main函数而言,一般情况下有如下的特性。
The   main   function   marks   the   beginning   and   end   of   program   execution.   A   C   or   C++   program   must   have   one   function   named   main.
main的原型可以是main(   int   argc,   char   *argv[   ],   char   *envp[   ]   )
那些参数都是系统传递给它的,而且不一定要真正使用所有的这些参数。
它可以接受入口参数说明:
argc

An   integer   specifying   how   many   arguments   are   passed   to   the   program   from   the   command   line.   Because   the   program   name   is   considered   an   argument,   argc   is   at   least   1.

argv

An   array   of   null-terminated   strings.   It   can   be   declared   as   an   array   of   pointers   to   char   (char   *argv[   ]   )   or   as   a   pointer   to   pointers   to   char   (char   **argv   ).   The   first   string   (argv[0])   is   the   program   name,   and   each   following   string   is   an   argument   passed   to   the   program   from   the   command   line.   The   last   pointer   (argv[argc])   is   NULL.

envp

A   pointer   to   an   array   of   environment   strings.   It   can   be   declared   as   an   array   of   pointers   to   char   (char   *envp[   ])   or   as   a   pointer   to   pointers   to   char   (char   **envp).     The   end   of   the   array   is   indicated   by   a   NULL   pointer.   The   environment   block   passed   to   main   is   a   “frozen”   copy   of   the   current   environment.   If   you   subsequently   change   the   environment   via   a   call   to   putenv   ,   the   current   environment   (as   returned   by   getenvand   the   _environvariable)   will   change,   but   the   block   pointed   to   by   envp   will   not   change.   This   argument   is   ANSI   compatible   in   C,   but   not   in   C++.

 

我没有研究过C99标准,也很少在非WINDOWS环境下编程。我不知道第三个参数是标准中的参数,还是VC编译器中独有的参数。

原创粉丝点击