When you write the main function, you'll often write it like this:
While correct, that's shorthand for this:
int main (int argc, char* argv[])
{
...
}
You have two variables here,
argc and
argv. The former is the number of parameters you pass to the application, and the latter the parameters themselves. Put another way, if we start our program like this:
program.exe My name "is Fred"
We have four parameters, so
argc == 4. The first is stored in
argv[0], and is the name of the program itself:
program.exe. The second, in
argv[1], is
My. The last two, in
argv[2] and
argv[3] respectively, are
name and
is Fred.
That's all a bit abstract, so we'll write a program to demonstrate. Compile and run this - call it "params.exe" or something:
#include <iostream>
int main (int argc, char* argv[])
{
std::cout << "argc = " << argc << std::endl << std::endl;
for (int i = 0; i < argc; i++)
{
std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
}
} Run it with parameters - for example,
params.exe 10 foo "I like sausages" or something equally retarded - and see what happens. If you have any more questions, let us know.
