10

I ran into a function like this earlier:

int main(int argc, char **argv, char **argw){


}

Why is there a need for three arguments, and how does this actually work?

user1090614
  • 2,575
  • 6
  • 22
  • 27
  • 1
    Looks related to getting [environment variables](http://stackoverflow.com/questions/16765545/how-to-list-all-environment-variables-in-a-c-c-app) some more details would help. – Shafik Yaghmour Feb 21 '14 at 16:11
  • As I recall, the variable was indeed called envp. Does that mean **argw is not an argument provided from the commandline? – user1090614 Feb 21 '14 at 16:14
  • It is environment variables of your shell in Unix let's says. So it is not provided on the command line and it is not portable. OSX adds a 4th argument `apple`, which I cover in my answer linked above. – Shafik Yaghmour Feb 21 '14 at 16:20
  • This is *likely* an extension provided by the implementation. It is described in ISO/IEC 9899:201x §J.5.1 Common Extensions: "In a hosted environment, the main function receives a third argument, char *envp[], that points to a null-terminated array of pointers to char, each of which points to a string that provides information about the environment for this execution of the program (5.1.2.2.1)." – WhozCraig Feb 21 '14 at 16:24

2 Answers2

11

The third argument to main is normally called envp.

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

Many compilers provide a third argument to main, but it is not specified in the C standard, so using it is undefined behaviour. If you try to port the code to a platform that doesn't provide a third parameter the program will most likely fail.

Is char *envp[] as a third argument to main() portable

Community
  • 1
  • 1
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • 4
    More portably, environment variables can be accessed via the `getenv` function, though that requires knowing the name of the variable you want to refer to. In addition, POSIX specifies `extern char **environ;` (which for some reason isn't declared in any header). – Keith Thompson Feb 21 '14 at 16:22
0

I've seen these arguments before. My compiler places them there as well when starting in C++ code. I can tell you for a fact that they are not necessary in C++, although I can't say for sure in C. They look to be slots for a variables to be passed in to the function int main. One of type int, and two of type char. These variables would be passed in, generally by the user at the time the program is executed.

NanoCarp
  • 103
  • 1
  • 2
  • The second (and optional third) parameters are of type `char**`, not `char`. – Keith Thompson Feb 21 '14 at 16:20
  • @KeithThompson Unless it's different in C than in C++, that doesn't seem to be the case, as both seem to be `char (variable name)`, where the `**` is part of the variable name. Although, that is odd as it violates the rules of naming variables, unless the compiler sees the `*` as a 'wildcard'. – NanoCarp Feb 21 '14 at 16:24
  • It's the same in C and C++. Declaration syntax is a bit odd, but it's based (not entirely consistently) on the principle that declaration follows use. The syntax of `char **argv` is meant to suggest that `**argv` is of type `char`; it follows from that that `argv` is of type `char**`. The variable name is just `argv`; `**` isn't part of the variable name, it's part of the declaration syntax. – Keith Thompson Feb 21 '14 at 16:37
  • @KeithThompson Odd, but I think I understand. I'm still learning myself, and trying to lend a hand. I'd like to know, is my response indicating they aren't required accurate, or am I off base here? – NanoCarp Feb 21 '14 at 16:46
  • In C, the two standard forms are `int main(void)` and `int main(int argc, char *argv[])`. In C++, the first form is `int main()`. Equivalent forms may also be used; for example, as a parameter definition, `char *argv[]` is equivalent to `char **argv`. Implementations may, but need not, support other forms. – Keith Thompson Feb 21 '14 at 16:59
  • @KeithThompson I see. Thanks for helping me learn new things! – NanoCarp Feb 21 '14 at 17:28