It is currently Wed Sep 08, 2010 6:12 am

All times are UTC - 5 hours [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: why int main(int argc, char * argv[]) ???
PostPosted: Mon Jun 21, 2010 3:26 am 
Joined: Mon Jun 21, 2010 3:22 am
Posts: 2

Hey everyone I'm just wondering why we add these args for the main function. I tried the first tutorial with regular main() and it wouldn't work, but when I added the arguments it went fine. I'm just curious is there something about SDL that requires these arguments?


Offline
 Profile  
 Post subject: Re: why int main(int argc, char * argv[]) ???
PostPosted: Mon Jun 21, 2010 12:21 pm 
Site Admin
Joined: Tue Oct 28, 2008 9:55 am
Posts: 185
Location: Indiana

Yes, SDL requires it that way. Why? Not sure on the exact technical details, but SDLmain (which sort of does initial setup of the SDL environment) basically overrides the normal "main" function, and wraps it in its own.

What happens is that main gets defined as SDL_main in SDL_main.h:
#define main SDL_main

SDL_main is prototyped like this:
extern C_LINKAGE int SDL_main(int argc, char *argv[]);

Now, according to the OS, SDL_main will setup up IO stuff, and other things. That is why when you compile on Windows you automagically have Output redirected to stdout.txt and stderr.txt. So, for your SDL app to work, it must match the SDL_main prototype exactly.


Offline
 Profile  
 Post subject: Re: why int main(int argc, char * argv[]) ???
PostPosted: Wed Jun 23, 2010 12:30 am 
Joined: Mon Jun 21, 2010 3:22 am
Posts: 2

I see. That's interesting, I didn't know that SDL overrides the regular main function. Thanks for the response!


Offline
 Profile  
 Post subject: Re: why int main(int argc, char * argv[]) ???
PostPosted: Tue Aug 10, 2010 2:06 am 
Joined: Tue Aug 10, 2010 1:54 am
Posts: 3

I think i know that answer...
When u compile a code with the "main" function with this parameters, when u run the executable on the TERMINAL (linux) and pass some parameters, this 2 variables gets the parameters... for example...
comand line:
/home/MYUSER/blahblah/program -cp ABC

the program catch the 2 parameters that u passed ('-cp' and 'ABC') and throws 2 on argc (2 parameters was passed, '-cp' to argv[0] (with no ticks) and 'ABC' to argv[1]...
its a simple method and dont requires any special library or something...
sorry for bad english...
i hope to help.
cya :)


Offline
 Profile  
 Post subject: Re: why int main(int argc, char * argv[]) ???
PostPosted: Fri Aug 13, 2010 12:32 pm 
Joined: Thu Jun 18, 2009 11:47 pm
Posts: 33

Actually, the command used to run the program will always be the first parameter, so argc will be at least 1 and argv[0] will be the command.


Offline
 Profile  
 Post subject: Re: why int main(int argc, char * argv[]) ???
PostPosted: Tue Aug 17, 2010 9:12 am 
Site Admin
Joined: Tue Oct 28, 2008 9:55 am
Posts: 185
Location: Indiana

Post got off track a little bit. The original question was why main had to be written a certain way - which you can see my answer above. That really has nothing to do with the command line arguments passed. Thanks for the discussion on it though.


Offline
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


 Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: