Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

main(int argc, char** argv, char** envp)--how to use envp?

1,727 views
Skip to first unread message

Sean Yang

unread,
Jul 31, 2002, 7:34:58 PM7/31/02
to
Is char **envp a parameter to describe the environment parameters? If
so, how many char* are refered by this char**?
I want to know how to use the envp parameter. Can anyone give a short
sample code?

Sean Yang

unread,
Jul 31, 2002, 7:38:09 PM7/31/02
to
Sean Yang wrote:

> Is char **envp a parameter to describe the environment parameters? If
> so, how many char* are refered by this char**?

How can I get the length of the array char* envp[]? Would anyone give me
a piece of code to handle such operation?--retrieve the length of an
array with unknow length.

Rolf Magnus

unread,
Jul 31, 2002, 8:28:19 PM7/31/02
to
Sean Yang wrote:

> Is char **envp a parameter to describe the environment parameters? If
> so, how many char* are refered by this char**?

Just iterate through the array until you find a NULL pointer.

> I want to know how to use the envp parameter. Can anyone give a short
> sample code?

#include <stdio.h>

int main(int argc, char* argv[], char* envp[])
{
while(*envp) puts(*envp++);
return 0;
}

BTW: if you're looking for a specific environment variable (rather than for
a list of them), you're better off with the getenv() function.

Jean Tabel

unread,
Jul 31, 2002, 8:46:47 PM7/31/02
to
argv and envp are both NULL-terminated array of pointers to NULL-terminated
strings.

#include <stdio.h>

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

int n = 0;

while (envp[n] != NULL) {
printf("envp[%d]: <%s\n", n, envp[n]);
n++;
}

printf("Got %d environment variables\n", n);

return 0;
}

I hope this may be of some help.

"Sean Yang" <yan...@purdue.edu> wrote in message
news:3D4874E1...@purdue.edu...

0 new messages