
-----------------------------------
ScaryRat
Sat Aug 14, 2010 4:16 pm

int argc, char *argv[] vc++ 2010
-----------------------------------
What does this mean?
when many programmers declare their main function, there are always "int argc, char *argv[]" in the parameters :
int main (int argc, char *argv[]) {
}

Could anyone tell me what it means?
Btw is it declaring an integer and a pointer to an character array?

-----------------------------------
chrisbrown
Sat Aug 14, 2010 7:26 pm

Re: int argc, char *argv[] vc++ 2010
-----------------------------------
They are command-line arguments. argc is short for argument count, argv: argument values. 
char *argv[] is an array of strings, which are just character arrays with a '\0' at the end.

If your compiled program is called app.exe, invoking [code]app.exe abc 123[/code]
results in argc = 2, argv[0] = "abc" and argv[1] = "123". Notice that 123 is a string, not an int.

-----------------------------------
DtY
Sat Aug 14, 2010 8:45 pm

Re: int argc, char *argv[] vc++ 2010
-----------------------------------
They are command-line arguments. argc is short for argument count, argv: argument values. 
char *argvargc would equal three; the first argument (argv[0] is always the command that was run that started the application)

so,
argc = 3
argv[0] = "app.exe"
argv[1] = "abc"
argv[2] = "123"

-----------------------------------
chrisbrown
Sun Aug 15, 2010 8:12 am

RE:int argc, char *argv[] vc++ 2010
-----------------------------------
My mistake, thanks for catching that.

-----------------------------------
ScaryRat
Tue Aug 24, 2010 9:42 am

RE:int argc, char *argv[] vc++ 2010
-----------------------------------
Thanks!
Sorry for this question but what specifically is an argument?

-----------------------------------
TheGuardian001
Tue Aug 24, 2010 12:17 pm

Re: int argc, char *argv[] vc++ 2010
-----------------------------------
Arguments are bits of data given to the program when it is launched. For example, if you open a .txt file (or if you launch it from the command line), the command given to Windows is:

[code]
notepad myFileName.txt
[/code]

In which case "notepad" is the command, and "myFileName.txt" is the argument. So in this case,

[code]
argv[0] = "C:\location_of_notepad\notepad.exe"
argv[1] = "myFileName.txt"
[/code]

No arguments are generally necessary to simply launch a program, however they are used to provide extra bits of information to the program which generally act as instructions for the program, whether it is a file to open, or a specific thing to do once they open.

-----------------------------------
ScaryRat
Mon Aug 30, 2010 9:20 am

RE:int argc, char *argv[] vc++ 2010
-----------------------------------
I thought that you cannot pass arrays (argv[]) into functions (int main)?

-----------------------------------
TerranceN
Mon Aug 30, 2010 9:56 am

RE:int argc, char *argv[] vc++ 2010
-----------------------------------
Go here and scroll down to "Arrays as Parameters".

-----------------------------------
DtY
Mon Aug 30, 2010 4:47 pm

Re: RE:int argc, char *argv[] vc++ 2010
-----------------------------------
I thought that you cannot pass arrays (argvIn a function definition argv[] is equivalent to *argv, it's just a convenient way to point out that that argument is an array (*argv[] is also the same as **argv).
