Advertising (This ad goes away for registered users. You can Login or Register)

Why is the main like this?

Programming on your favorite platform, for your favorite platform? Post here
Post Reply
waratte
Posts: 1320
Joined: Wed Oct 20, 2010 12:03 am

Why is the main like this?

Post by waratte » Mon Mar 28, 2011 3:19 pm

While I was creating a rougelike, I took a break and started playing with the main method. Why does it only accept two arguments or none at all? Why not 1 or three or more?
Advertising

electrosheep
Posts: 97
Joined: Tue Jan 11, 2011 2:50 am

Re: Why is the main like this?

Post by electrosheep » Tue Mar 29, 2011 2:53 am

I think you need to be a little more descriptive. You were creating a rougelike, which I have never heard of, but google was my friend and you must be talking about this. What language are you coding in?

EDIT: Actually maybe you were descriptive enough, and it's my lack of programming knowledge that's the problem. I assume you are programming in either c or c++?

EDIT: You might check this out.
Advertising
Let me see...your grandmother's name was Beatrice?

waratte
Posts: 1320
Joined: Wed Oct 20, 2010 12:03 am

Re: Why is the main like this?

Post by waratte » Tue Mar 29, 2011 3:07 am

electrosheep wrote:I think you need to be a little more descriptive. You were creating a rougelike, which I have never heard of, but google was my friend and you must be talking about this. What language are you coding in?

EDIT: Actually maybe you were descriptive enough, and it's my lack of programming knowledge that's the problem. I assume you are programming in either c or c++?

EDIT: You might check this out.
A rougelike is a genre of text based games. Take T.o.M.E. for PSP as an example. It doesn't have anything to do with my question though, I just like stories...

electrosheep
Posts: 97
Joined: Tue Jan 11, 2011 2:50 am

Re: Why is the main like this?

Post by electrosheep » Tue Mar 29, 2011 3:14 am

Yeah, you're right, it doesn't have anything to do with your question. I shouldn't have brought it up. I just saw that no one responded to your question, so I figured you must not have been clear enough. Hope that *** I linked helped.
Let me see...your grandmother's name was Beatrice?

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Why is the main like this?

Post by m0skit0 » Tue Mar 29, 2011 7:38 am

I guess you don't know what are those arguments used for. I'll try to explain.

Code: Select all

int main(int argc, char* argv[])
argc is how much arguments were passed to the application.
argv[] is an array of strings of all arguments passed.

Why it does have two instead of 3 or none? Because that's the needed number of arguments to fulfill its purpose, that is, passing arguments given to the application to the application itself. The OS takes care of filling those arguments for the application to process them.
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

waratte
Posts: 1320
Joined: Wed Oct 20, 2010 12:03 am

Re: Why is the main like this?

Post by waratte » Tue Mar 29, 2011 7:55 am

m0skit0 wrote:I guess you don't know what are those arguments used for. I'll try to explain.

Code: Select all

int main(int argc, char* argv[])
argc is how much arguments were passed to the application.
argv[] is an array of strings of all arguments passed.

Why it does have two instead of 3 or none? Because that's the needed number of arguments to fulfill its purpose, that is, passing arguments given to the application to the application itself. The OS takes care of filling those arguments for the application to process them.
I understand now, thanks m0skit0!

User avatar
FrEdDy
HBL Collaborator
Posts: 243
Joined: Mon Sep 27, 2010 7:08 pm
Contact:

Re: Why is the main like this?

Post by FrEdDy » Tue Mar 29, 2011 5:25 pm

m0skit0 wrote:I guess you don't know what are those arguments used for. I'll try to explain.

Code: Select all

int main(int argc, char* argv[])
argc is how much arguments were passed to the application.
argv[] is an array of strings of all arguments passed.

Why it does have two instead of 3 or none? Because that's the needed number of arguments to fulfill its purpose, that is, passing arguments given to the application to the application itself. The OS takes care of filling those arguments for the application to process them.
well,don't forget about char **envp,even thought nobody uses it.
https://github.com/freddy-156
<@n00b81> FREDDY CUTTIES

Babkock
Posts: 36
Joined: Sat Mar 12, 2011 10:30 pm

Re: Why is the main like this?

Post by Babkock » Tue Mar 29, 2011 9:24 pm

waratte wrote:
m0skit0 wrote:I guess you don't know what are those arguments used for. I'll try to explain.

Code: Select all

int main(int argc, char* argv[])
argc is how much arguments were passed to the application.
argv[] is an array of strings of all arguments passed.

Why it does have two instead of 3 or none? Because that's the needed number of arguments to fulfill its purpose, that is, passing arguments given to the application to the application itself. The OS takes care of filling those arguments for the application to process them.
I understand now, thanks m0skit0!
When you invoke your program through a terminal, you can pass arguments to argv[1], argv[2], and such by executing the command with arguments after it, separated by spaces. Say you type this into a terminal.

Code: Select all

$ ./prog.exe hello world
argv[0] == "./prog.exe"
argv[1] == "hello"
argv[2] == "world"
argv[3] == NULL
argv[4] == NULL

and so on. Using the above command, argc will either be 2 or 3, I can't remember, it always trips me up.
My latest homebrew: ToneMatrix 0.9 - Now with export feature!

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Why is the main like this?

Post by m0skit0 » Tue Mar 29, 2011 9:28 pm

FrEdDy wrote:don't forget about char **envp
AFAIK that's not standard.
Babkock wrote:Using the above command, argc will either be 2 or 3, I can't remember, it always trips me up.
3
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

Post Reply

Return to “Programming”