tags:

views:

39

answers:

1
+5  A: 

char **environ is NULL-terminated array of strings, so you should try:

extern char **environ;
char **p;
for (p = environ; *p; p++) {
    printf ("%s\n", *p);
}

In other words, environ[0] is pointer to first env variable, environ[1] to second etc. Last element in environ array is NULL.

el.pescado
And there's no guarantee that the list of variables is any particular order, and in some (unusual) circumstances, there might be two entries for a single variable. It used to be the case that the environment was also passed into POSIX-ish programs if you used `int main(int argc, char **argv, char **envp)` for the main function declaration. However, neither the 2003 nor the 2008 standard mentions this possibility, so I guess it is not guaranteed. MacOS X still supports it, and so probably do most variations of Unix (it was documented for 7th Edition Unix in 1979).
Jonathan Leffler