Valhalla Legends Forums Archive | General Programming | main function [C++]

AuthorMessageTime
MailMan
I've seen main functions with arguments before. ie:

[code]
int main(int something, int something_else)
[/code]

What's passed to the main function? Is that for like.. variables passed via command line or something?
March 28, 2003, 1:25 AM
iago
Standard main is:
int main(int argc, char *argv[])

argc is the number of commandline arguments, and argv is an array of the arguments, [0] being the name of the program.
March 28, 2003, 1:43 AM
MailMan
Cool, thanks!
March 28, 2003, 2:56 AM
Skywing
[quote author=iago link=board=5;threadid=835;start=0#msg6536 date=1048815806]
Standard main is:
int main(int argc, char *argv[])

argc is the number of commandline arguments, and argv is an array of the arguments, [0] being the name of the program.
[/quote]Don't forget:
[code]int main(int argc, char *argv[], char *envp[])[/code]
March 29, 2003, 5:41 AM
Zakath
I've not seen that one...what's the third param for? What would a second array of strings do there?
March 29, 2003, 7:34 AM
iago
I would guess they are environmental variables, seeing as though it's called envp :)
March 29, 2003, 9:22 AM
Yoni
Yes.

All three forms (0, 2, 3 parameters) are acceptable.
March 29, 2003, 10:33 AM
Skywing
[quote author=Yoni link=board=5;threadid=835;start=0#msg6620 date=1048934009]
Yes.

All three forms (0, 2, 3 parameters) are acceptable.
[/quote]void main() is not acceptable, however, despite what Visual C++ might try to tell you.
March 29, 2003, 5:02 PM
Kp
envp is Windows specific, iirc. Use getenv() instead.
March 30, 2003, 12:23 AM
Skywing
[quote author=Kp link=board=5;threadid=835;start=0#msg6652 date=1048983797]
envp is Windows specific, iirc. Use getenv() instead.
[/quote]Ah, you're right. According to documentation, I've found, it's something most Windows and UNIX compilers implement, but not a standard feature.
March 30, 2003, 1:10 AM
Etheran
[code]int main(int argc, wchar_t *argv[]);[/code]
is this not supported?
April 8, 2003, 9:01 AM
Yoni
[quote author=Etheran link=board=5;threadid=835;start=0#msg7305 date=1049792461]
[code]int main(int argc, wchar_t *argv[]);[/code]
is this not supported?
[/quote]If you want the argv argument to be a wide string, you'll have to rename main to wmain.

MSVC++ also allows the following:
[code]int _tmain(int argc, TCHAR* argv[]);[/code]

Similarly with WinMain:
[code]int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nShow);
// or
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nShow);
// or
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int nShow);[/code]
April 8, 2003, 2:30 PM
MrRaza
I've also seen [code]void main(void) {...}[/code] (This is with C btw) is that legal? And add preformated text tags, i dont like how [ code][/code ] creates a new line....
April 9, 2003, 12:00 AM
Eibro
[quote author=MrRaza link=board=5;threadid=835;start=0#msg7335 date=1049846431]
I've also seen [code]void main(void) {...}[/code] (This is with C btw) is that legal? And add preformated text tags, i dont like how [ code][/code ] creates a new line....
[/quote]No, that's not standard C (or C++). http://www.eskimo.com/~scs/readings/voidmain.960823.html
void main is simply wrong.
April 9, 2003, 12:11 AM
MrRaza
Well, i guess my C++ book is wrong, but hey atleast it has some good AI information in it..
April 9, 2003, 12:32 AM

Search