learner said:
Hi,
What is the difference between accessing environment
variables through
int main(int argc,char *argv[],char *envp[])
and
extern char **environ;
No difference, they are both non-standard and unportable. In fact, environ
is not a reserved identifier so any attempt by the implementation to supply
a definition of it, let alone any value other than the usual null-pointer
initialisation, would be improper.
Cygwin's GCC 3.3.1 appears to have a bug! If I try to define my own extern
variable, I get this error:
/usr/lib/gcc-lib/i686-pc-cygwin/3.3.1/../../../libcygwin.a(_cygwin_crt0_comm
on.o)(.bss+0x0): multiple definition of `_environ'
/cygdrive/c/DOCUME~1/SIMONB~1/LOCALS~1/Temp/ccMMv8Qk.o(.data+0x0):environ2.c
: first defined here
I should be allowed to create my own 'extern' variable -- the name is not
reserved.
MS's CL 13.10.3052 does provide a definition of environ, but its linker also
allows me to create my own, which overrides the internal one. This is
probably standard-conforming under the 'as-if' rule.
Borland BCC32 5.5.1 correctly gives an error if environ is used but not
defined:
Error: Unresolved external '_environ' referenced from
C:\DOCUMENT\PROG\C\ENVIRON.OBJ
LCC-Win32 correctly behaves like bcc32, in that I get a linker error that
environ is not defined:
environ.obj .text: undefined reference to '_environ'
linker returned 2
but for some weird reason it goes on to create an executable anyway! The
executable crashes, for what it's worth.
Is there any advantage in any of the above two?
No. I suggest you use the standard C solution, which is a library function:
char *getenv(const char *name);
It will search the list for an environment variable of the name given, and
return a pointer to a string containing the value of that variable. If the
name is not found, a null pointer will be returned.