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?

4
  • 1
    Looks related to getting environment variables some more details would help. Commented Feb 21, 2014 at 16:11
  • As I recall, the variable was indeed called envp. Does that mean **argw is not an argument provided from the commandline? Commented Feb 21, 2014 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. Commented Feb 21, 2014 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)." Commented Feb 21, 2014 at 16:24

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

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).
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.

6 Comments

The second (and optional third) parameters are of type char**, not char.
@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'.
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.
@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?
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.