• Welcome to Valhalla Legends Archive.
 

main function [C++]

Started by MailMan, March 27, 2003, 07:25 PM

Previous topic - Next topic

MailMan

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


int main(int something, int something_else)


What's passed to the main function? Is that for like.. variables passed via command line or something?

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,
  • being the name of the program.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


MailMan


Skywing

Quote from: iago on March 27, 2003, 07:43 PM
Standard main is:
int main(int argc, char *argv[])

argc is the number of commandline arguments, and argv is an array of the arguments,
  • being the name of the program.
Don't forget:
int main(int argc, char *argv[], char *envp[])

Zakath

I've not seen that one...what's the third param for? What would a second array of strings do there?
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

iago

I would guess they are environmental variables, seeing as though it's called envp :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Yoni

Yes.

All three forms (0, 2, 3 parameters) are acceptable.

Skywing

Quote from: Yoni on March 29, 2003, 04:33 AM
Yes.

All three forms (0, 2, 3 parameters) are acceptable.
void main() is not acceptable, however, despite what Visual C++ might try to tell you.

Kp

envp is Windows specific, iirc.  Use getenv() instead.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Skywing

Quote from: Kp on March 29, 2003, 06:23 PM
envp is Windows specific, iirc.  Use getenv() instead.
Ah, you're right.  According to documentation, I've found, it's something most Windows and UNIX compilers implement, but not a standard feature.

Etheran

int main(int argc, wchar_t *argv[]);
is this not supported?

Yoni

#11
Quote from: Etheran on April 08, 2003, 04:01 AM
int main(int argc, wchar_t *argv[]);
is this not supported?
If you want the argv argument to be a wide string, you'll have to rename main to wmain.

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

Similarly with WinMain:
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);

MrRaza

I've also seen void main(void) {...} (This is with C btw) is that legal? And add preformated text tags, i dont like how [ code][/code ] creates a new line....

Eibro

Quote from: MrRaza on April 08, 2003, 07:00 PM
I've also seen void main(void) {...} (This is with C btw) is that legal? And add preformated text tags, i dont like how [ code][/code ] creates a new line....
No, that's not standard C (or C++). http://www.eskimo.com/~scs/readings/voidmain.960823.html
void main is simply wrong.
Eibro of Yeti Lovers.

MrRaza

Well, i guess my C++ book is wrong, but hey atleast it has some good AI information in it..