I have come to understand that char **envp
is the third argument to main
, and with the help of the code below, I was able to see what it actually contains.
int main(int argc, char *argv[], char *env[])
{
int i;
for (i=0 ; env[i] ; i++)
cout << env[i] << endl;
cout << endl;
}
My question is: why (in what situations) would programmers need to use this? I have found many explanations for what this argument does, but nothing that would tell me where this is typically used. Trying to understand what kind of real world situations this might be used in.
This is typically used to set configuration options or other information for a whole group of programs. Another use is to specify environment settings for a particular machine or user setup.
Well known examples are the
PATH
variable that contains the lookup pathes for executables, or theLD_LIBRARY_PATH
variable that contains the pathes where to lookup the shared libraries.The
getenv()
function allows you to find the value of a specific environment variable, but doesn't provide a mechanism to scan over the entire list of environment variables. Theenvp
argument allows you to iterate over the entire list of environment variables, as your demonstration code shows which is simply not feasible using thegetenv()
interface.On POSIX systems, there is a global variable,
extern char **environ;
, which also points to the environment. The functionsputenv()
(ancient, non-preferred because it presents memory management problems),setenv()
andunsetenv()
can also manipulate the environment variable list (as defined byenviron
). A program can directly modifyenviron
or the values it points at, but that is not advisable.If you are using
fork()
and theexec*()
family of functions, unless you useexecve()
and specify the environment explicitly, the child process will receive the environment defined byenviron
.No header declares
environ
— AFAIK, it is the only variable defined by POSIX without a header to declare it. The C standard recognizes theint main(int argc, char **argv, char **envp)
signature formain()
as a common extension to the standard, documented in Annex J.env
allows you to access the environment variables. It contains an array of strings. Examples are the users home directory, the configured language scheme, the PATH variable (where to look for directly executable programs?), ...You can also set individual environment variables. For example, if you have testing (learning) and also a production system you deploy your application to. On one system you could set the variable "MY_APP_MODE=TEST" and on the other system you could specify "MY_APP_MODE=PROD". So you don't need to deploy different applications to the test and production systems. Your application could determine itself in what environment it is run.
It is an array containing all the environmental variables. It can be used for example to get the user name or home directory of current logged in user. One situation is, for example, if I want to hold a configuration file in user's home directory and I need to get the PATH;
Equivalent of
env
is char* getenv (const char* name) function which is easier to use, for example:prints user name of current user.