Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Under many hosted environments it is possible to access the environment through a modified form of main():

Code Block

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

Wiki Markup
According to C99: \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\]:

Wiki Markup
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.

So under a hosted environments it is possible to access the environment through a modified form of main():

Code Block

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

Wiki Markup
However, modifying the environment by using the {{setenv()}} or {{putenv()}} functions, or by any other means, may cause the environment memory to be reallocated, with the result that {{envp}} now references an incorrect location. For example, POSIX says the following: \[[Open Group 04|AA. C References#Open Group 04]\]

Unanticipated results may occur if setenv() changes the external variable environ.  In particular, if the optional envp argument to main() is present, it is not changed, and as a result may point to an obsolete copy of the environment (as may any other copy of environ).

Wiki Markup
Microsoft notes the following about {{getenv()}}: \[[MSDN|AA. C References#MSDN]\]
According to the Microsoft Visual Studio 2005/.NET Framework 2.0 help pages:

The getenv function searches the list of environment variables for varname. getenv is not case sensitive in the Windows operating system. getenv and _putenv use the copy of the environment pointed to by the global variable _environ to access the environment. getenv operates only on the data structures accessible to the runtime library and not on the environment "segment" created for the process by the operating system. Consequently, programs that use the envp argument to main or wmain may retrieve invalid information.

...

Wiki Markup
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section J.5.1, "Environment Arguments"
\[[MSDN|AA. C References#MSDN]\] [getenv, _wgetenv|http://msdn.microsoft.com/en-us/library/tehxacec.aspx]
\[[Open Group 04|AA. C References#Open Group 04]\] [{{setenv()}}|http://www.opengroup.org/onlinepubs/009695399/functions/setenv.html]

...