Page 1 of 1

question: Why does main need parameters?

Posted: Mon May 24, 2010 11:40 am
by Avishaiozeri
Hi guys, i have a question. Lately i'm starting to learn Sdl more seriously, and now when i practice i'm getting used to adding the (int argc, char* args[]) to the main as parameters. Why do i need these? is it some parameters the operating system needs to send the main function in order to communicate with it or something?
Thanks

Re: question: Why does main need parameters?

Posted: Mon May 24, 2010 11:47 am
by short
argc is the number or arguments in argv.

argv is an array of strings, with i believe the first one argv[0] being the name of your program. Debug your program and inspect the contents on the argv array.

I believe sdl needs them for some of its internal workings, but I am not positive on that. GOOGLE! :)

Re: question: Why does main need parameters?

Posted: Mon May 24, 2010 11:47 am
by Milch
This is for additional arguments for your program.
Like the '-console' for Source games to enable console on startup.

http://www.site.uottawa.ca/~lucia/cours ... ments.html

Re: question: Why does main need parameters?

Posted: Mon May 24, 2010 12:03 pm
by Avishaiozeri
Milch wrote:This is for additional arguments for your program.
Like the '-console' for Source games to enable console on startup.

http://www.site.uottawa.ca/~lucia/cours ... ments.html
I'v tried the link you gave, and used the code they wrote there to check what is in the argv array.
I saw it contains the path to the software on my computer. thank you. but i have another question,
do i need to put these parameters if i compile the project for other systems, like linux or something?

Re: question: Why does main need parameters?

Posted: Mon May 24, 2010 12:08 pm
by cndr
you do need the parameters (int argc, char *argv[]) for linux.

Re: question: Why does main need parameters?

Posted: Mon May 24, 2010 12:17 pm
by Avishaiozeri
cndr wrote:you do need the parameters (int argc, char *argv[]) for linux.
Does linux has same file paths as windows? And if it does, what about different type of machine? like dreamcast or ps?

Re: question: Why does main need parameters?

Posted: Mon May 24, 2010 12:37 pm
by ismetteren
EDIT: i dident realize the link posted by Milch contained all the information... but now when i have used time on writing it, i will let it be here ;)
Avishaiozeri wrote:
cndr wrote:you do need the parameters (int argc, char *argv[]) for linux.
Does linux has same file paths as windows? And if it does, what about different type of machine? like dreamcast or ps?
No linux uses / instead of \ and you just start with a / to indicate root. No such thing as drives.

argv contains arguments you can send to your program on startup. Usually you start your program like this:

Code: Select all

>myprogram.exe
You can pass args to the program like this:

Code: Select all

>myprogram.exe arg1 arg2 arg3
You could then do like this in your program:

Code: Select all

for(int i = 1; i<argc; ++i) {
    cout << argv[i] << endl;
}
this would the output:

Code: Select all

arg1
arg2
arg3
if you ran your program like this:

Code: Select all

>myprogram.exe hello im ismetteren
it would output this:

Code: Select all

hello
im
ismetteren
nearly all command line tools i know uses args. ant, git, svn, make and so on.

Re: question: Why does main need parameters?

Posted: Mon May 24, 2010 12:57 pm
by Avishaiozeri
ismetteren wrote:EDIT: i dident realize the link posted by Milch contained all the information... but now when i have used time on writing it, i will let it be here ;)
Avishaiozeri wrote:
cndr wrote:you do need the parameters (int argc, char *argv[]) for linux.
Does linux has same file paths as windows? And if it does, what about different type of machine? like dreamcast or ps?
No linux uses / instead of \ and you just start with a / to indicate root. No such thing as drives.

argv contains arguments you can send to your program on startup. Usually you start your program like this:

Code: Select all

>myprogram.exe
You can pass args to the program like this:

Code: Select all

>myprogram.exe arg1 arg2 arg3
You could then do like this in your program:

Code: Select all

for(int i = 1; i<argc; ++i) {
    cout << argv[i] << endl;
}
this would the output:

Code: Select all

arg1
arg2
arg3
if you ran your program like this:

Code: Select all

>myprogram.exe hello im ismetteren
it would output this:

Code: Select all

hello
im
ismetteren
nearly all command line tools i know uses args. ant, git, svn, make and so on.
Thank you, but does the dreamcast and ps has this thing?
Edit: The games that people tell you to add things like "-window" and stuff to the shortcut path, to make them act differently, is this
the arg?

Re: question: Why does main need parameters?

Posted: Mon May 24, 2010 2:51 pm
by Milch
Avishaiozeri wrote: Edit: The games that people tell you to add things like "-window" and stuff to the shortcut path, to make them act differently, is this
the arg?
Yes.
So you can use the args for stuff like skipping the main menu and loading a level, starting the programm in window mode and so on.

Re: question: Why does main need parameters?

Posted: Mon May 24, 2010 2:59 pm
by Ginto8
Avishaiozeri wrote:does the dreamcast and ps has this thing?
they probably have something along these lines, but a bit different. I know that GBA and DS homebrew, for the most part don't use them, but I'm not sure how they work if you do use them.

Re: question: Why does main need parameters?

Posted: Tue May 25, 2010 4:56 am
by Avishaiozeri
All my questions were answered, thank you all! :)

Re: question: Why does main need parameters?

Posted: Tue May 25, 2010 6:53 am
by RyanPridgeon
It's also useful to know for Windows, that If you right click on a file and choose Open With, then choose your program, the path to that file will be stored in argv[1].

Re: question: Why does main need parameters?

Posted: Tue May 25, 2010 7:30 am
by Avishaiozeri
So, it can be used to determine the format your program will support?

Re: question: Why does main need parameters?

Posted: Tue May 25, 2010 9:25 am
by dandymcgee
RyanPridgeon wrote:It's also useful to know for Windows, that If you right click on a file and choose Open With, then choose your program, the path to that file will be stored in argv[1].
Or if you drag and drop a file onto your program in explorer. :)

Re: question: Why does main need parameters?

Posted: Tue May 25, 2010 7:31 pm
by eatcomics
Never knew that, that could be handy for a map editor or something similar :P
Just drag and drop your map and it opens up