Reputation: 5049
We have
int main(int argc, char** argv, char** envc)
for the ordinary. but I want to know if there is any other argument main can have instead of these. And if there is any, what does it point to?
Upvotes: 3
Views: 1036
Reputation: 264361
It is an extension to the standard:
But it is supposed to provide access to the environment:
int main(int argc, char** argv, char** envc)
{
// It is an array of pointers to C-String
// The array is terminated with a NULL pointer.
// So you can loop over with it like this.
for(int loop = 0;envc[loop] != NULL; ++loop)
{
fprintf(stdout, "%s\n", envc[loop]);
}
}
It is probably better to use getenv
:
char * getenv ( const char * name );
http://www.cplusplus.com/reference/clibrary/cstdlib/getenv/
Upvotes: 1
Reputation: 476960
The answers differ in C and in C++:
In C++, main
must always return int
. Every implementation must accept ()
and (int, char**)
signatures. An implementation may accept any other signature. If an accepted signature begins with int, char**, ...
, those should have the usual meaning. (Also, main
gets C linkage, mustn't be overloaded, mustn't be a template, and mustn't be called.)
In C, main
may take any form. However, every implementation must accept int(void)
and int(int, char**)
types.
As you have noticed, one popular signature supported by certain environments, and conforming with these guidelines, is int main(int argc, char * argv[], char * env[])
, in which the third argument contains a pointer to the environment. Other extensions are conceivable; check the documentation of your platform.
Upvotes: 4
Reputation:
I think this answers your question:
http://en.wikipedia.org/wiki/Main_function#C_and_C.2B.2B
Upvotes: 2
Reputation: 35783
Well, heres the breakdown:
argc
-- C standardargv
-- C standardenv
-- Works on most UNIX and MS Win, but not standardapple
-- Other information passed as forth argument by Mac OSX and DarwinUpvotes: 8
Reputation: 64308
Only argc and argv are standard arguments. Anything after that depends on your system and compiler.
Upvotes: 3