1

What does char * arge[] mean as third parameter in the main function in a C program ?

#include <stdio.h>
#include <unistd.h>

int main(int argc, char const *argv[], char *arge[]) {
  int i;
  char **p;
  printf("pid = %d\n", getpid());
  printf("argc =  %d\n", argc);
  for (i = 0; i < argc; i++)
  printf("%s\n",argv[i] );
  p=arge;
  while (*p != NULL)
  printf("%s\n", *p++);
  execve("prog2", argv, arge);
  return 0;
}

"prog2" mentionned is this one:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char const *argv[], char *arge[]) {
  printf("pid = %d\n", getpid());
  printf("argc = %d\n", argc);
  for(;;);
}

If you execute the code you will get all the environment variables.

Paul Lecomte
  • 141
  • 1
  • 10
  • 2
    Possible duplicate of [What's the use of the third, environment variable argument to the C++ main()?](https://stackoverflow.com/questions/19198797/whats-the-use-of-the-third-environment-variable-argument-to-the-c-main) (finders credits to @mort) – Yunnosch Oct 06 '19 at 09:25
  • I added the code of my C program and what it returns – Paul Lecomte Oct 06 '19 at 09:57
  • @Yunnosch the question then is what's the difference btw "arge[]" and "envp[]" – Paul Lecomte Oct 06 '19 at 09:59

1 Answers1

1

The third argument is environment variables. It's answered in more depth here: What's the use of the third, environment variable argument to the C++ main()?

Getting environment variables as a third argument to main seems to be specified in neither C nor POSIX though, so it's potentially not portable: Is char *envp[] as a third argument to main() portable

mort
  • 704
  • 2
  • 9
  • 21
  • While I learned something new (or better old) you should have flagged as duplicate instead of answering. – Yunnosch Oct 06 '19 at 09:24
  • The link which answers the question doesn't mention that it's am extension and not C or POSIX though, which I feel is important information. – mort Oct 06 '19 at 09:31
  • I do agree that this info is important. However, the Q/A pair you found is better than you think. Comments and "other" answers do provide that information. – Yunnosch Oct 06 '19 at 09:40
  • Or maybe the other proposed duplicate is more to your taste. – Yunnosch Oct 06 '19 at 09:42
  • @mort you should right, this is an environment variables, the name given is just different. "argE" E like environment .. – Paul Lecomte Oct 06 '19 at 09:50