question: Why does main need parameters?
Moderator: Coders of Rage
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Wed Mar 17, 2010 4:32 pm
question: Why does main need parameters?
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
Thanks
- short
- ES Beta Backer
- Posts: 548
- Joined: Thu Apr 30, 2009 2:22 am
- Current Project: c++, c
- Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
- Programming Language of Choice: c, c++
- Location: Oregon, US
Re: question: Why does main need parameters?
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!
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!
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
- Milch
- Chaos Rift Junior
- Posts: 241
- Joined: Sat Jul 11, 2009 5:55 am
- Programming Language of Choice: C++
- Location: Austria, Vienna
Re: question: Why does main need parameters?
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
Like the '-console' for Source games to enable console on startup.
http://www.site.uottawa.ca/~lucia/cours ... ments.html
Follow me on twitter!
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Wed Mar 17, 2010 4:32 pm
Re: question: Why does main need parameters?
I'v tried the link you gave, and used the code they wrote there to check what is in the argv array.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 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?
- cndr
- Chaos Rift Cool Newbie
- Posts: 65
- Joined: Sat Apr 24, 2010 7:03 am
- Programming Language of Choice: etc.
Re: question: Why does main need parameters?
you do need the parameters (int argc, char *argv[]) for linux.
Long Live The Queen!
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Wed Mar 17, 2010 4:32 pm
Re: question: Why does main need parameters?
Does linux has same file paths as windows? And if it does, what about different type of machine? like dreamcast or ps?cndr wrote:you do need the parameters (int argc, char *argv[]) for linux.
- ismetteren
- Chaos Rift Junior
- Posts: 276
- Joined: Mon Jul 21, 2008 4:13 pm
Re: question: Why does main need parameters?
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
argv contains arguments you can send to your program on startup. Usually you start your program like this:
You can pass args to the program like this:
You could then do like this in your program:
this would the output:
if you ran your program like this:
it would output this:
nearly all command line tools i know uses args. ant, git, svn, make and so on.
No linux uses / instead of \ and you just start with a / to indicate root. No such thing as drives.Avishaiozeri wrote:Does linux has same file paths as windows? And if it does, what about different type of machine? like dreamcast or ps?cndr wrote:you do need the parameters (int argc, char *argv[]) for linux.
argv contains arguments you can send to your program on startup. Usually you start your program like this:
Code: Select all
>myprogram.exe
Code: Select all
>myprogram.exe arg1 arg2 arg3
Code: Select all
for(int i = 1; i<argc; ++i) {
cout << argv[i] << endl;
}
Code: Select all
arg1
arg2
arg3
Code: Select all
>myprogram.exe hello im ismetteren
Code: Select all
hello
im
ismetteren
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Wed Mar 17, 2010 4:32 pm
Re: question: Why does main need parameters?
Thank you, but does the dreamcast and ps has this thing?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
No linux uses / instead of \ and you just start with a / to indicate root. No such thing as drives.Avishaiozeri wrote:Does linux has same file paths as windows? And if it does, what about different type of machine? like dreamcast or ps?cndr wrote:you do need the parameters (int argc, char *argv[]) for linux.
argv contains arguments you can send to your program on startup. Usually you start your program like this:You can pass args to the program like this:Code: Select all
>myprogram.exe
You could then do like this in your program:Code: Select all
>myprogram.exe arg1 arg2 arg3
this would the output:Code: Select all
for(int i = 1; i<argc; ++i) { cout << argv[i] << endl; }
if you ran your program like this:Code: Select all
arg1 arg2 arg3
it would output this:Code: Select all
>myprogram.exe hello im ismetteren
nearly all command line tools i know uses args. ant, git, svn, make and so on.Code: Select all
hello im ismetteren
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?
- Milch
- Chaos Rift Junior
- Posts: 241
- Joined: Sat Jul 11, 2009 5:55 am
- Programming Language of Choice: C++
- Location: Austria, Vienna
Re: question: Why does main need parameters?
Yes.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?
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.
Follow me on twitter!
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: question: Why does main need parameters?
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.Avishaiozeri wrote:does the dreamcast and ps has this thing?
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Wed Mar 17, 2010 4:32 pm
Re: question: Why does main need parameters?
All my questions were answered, thank you all!
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: question: Why does main need parameters?
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].
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Wed Mar 17, 2010 4:32 pm
Re: question: Why does main need parameters?
So, it can be used to determine the format your program will support?
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: question: Why does main need parameters?
Or if you drag and drop a file onto your program in explorer.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].
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
Re: question: Why does main need parameters?
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
Just drag and drop your map and it opens up