Some implomentations implementations provide a nonportable environment pointer that is valid when main()
is called but may be invalidated by operations that modify the environment.
The C Standard, J.5.1 [ISO/IEC 9899:2011], states:
In a hosted environment, the main function receives a third argument,
char *envp[]
, that points to a null-terminated array of pointers tochar
, each of which points to a string that provides information about the environment for this execution of the program.
...
After a call to the POSIX setenv()
function or to another function that modifies the environment, the envp
pointer may no longer reference the current environment. The Portable Operating System Interface (POSIX®), Base Specifications, Issue 7 [IEEE Std 1003.1:2013], states:
Unanticipated results may occur if
setenv()
changes the external variableenviron
. In particular, if the optionalenvp
argument tomain()
is present, it is not changed, and thus may point to an obsolete copy of the environment (as may any other copy ofenviron
).
...