Computer Science Canada

int argc, char *argv[] vc++ 2010

Author:  ScaryRat [ Sat Aug 14, 2010 4:16 pm ]
Post subject:  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?

Author:  chrisbrown [ Sat Aug 14, 2010 7:26 pm ]
Post subject:  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

results in argc = 2, argv[0] = "abc" and argv[1] = "123". Notice that 123 is a string, not an int.

Author:  DtY [ Sat Aug 14, 2010 8:45 pm ]
Post subject:  Re: int argc, char *argv[] vc++ 2010

chrisbrown @ Sat Aug 14, 2010 7:26 pm wrote:
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

results in argc = 2, argv[0] = "abc" and argv[1] = "123". Notice that 123 is a string, not an int.
argc 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"

Author:  chrisbrown [ Sun Aug 15, 2010 8:12 am ]
Post subject:  RE:int argc, char *argv[] vc++ 2010

My mistake, thanks for catching that.

Author:  ScaryRat [ Tue Aug 24, 2010 9:42 am ]
Post subject:  RE:int argc, char *argv[] vc++ 2010

Thanks!
Sorry for this question but what specifically is an argument?

Author:  TheGuardian001 [ Tue Aug 24, 2010 12:17 pm ]
Post subject:  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


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"


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.

Author:  ScaryRat [ Mon Aug 30, 2010 9:20 am ]
Post subject:  RE:int argc, char *argv[] vc++ 2010

I thought that you cannot pass arrays (argv[]) into functions (int main)?

Author:  TerranceN [ Mon Aug 30, 2010 9:56 am ]
Post subject:  RE:int argc, char *argv[] vc++ 2010

Go <a href="http://www.cplusplus.com/doc/tutorial/arrays/">here</a> and scroll down to "Arrays as Parameters".

Author:  DtY [ Mon Aug 30, 2010 4:47 pm ]
Post subject:  Re: RE:int argc, char *argv[] vc++ 2010

ScaryRat @ Mon Aug 30, 2010 9:20 am wrote:
I thought that you cannot pass arrays (argv[]) into functions (int main)?
In 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).


: