as you know, every c++ program has a main function. The main function has two parameters that you have not seen yet, because none of the programs have defined those parameters. All the programs so far have defined main with a empty parameter list. But main does indeed have two parameters. The two parameters are an int and a pointer to an array of char pointers the int parameter contains the number of command line arguments that the user types on the command line to run the program. The char*[] argument points to an array of character pointers, which themselves point to the textual command-line arguments although you may name these two parameters anything you like, the convention is to nmae them argc and argv and to declare then in the main function header <pre class='_prettyXprint _lang-auto _linenums:0'>int main()
{
//...
}</pre>the argc parameter always has a count of at least 1, and has a higher count if the user types arguments on the command line. There is always at least one char pointer in the array pointed to by argv, and it, argv[], points to the name of the program's executable file. if argc is greater then 1, the following argv parameters, argv[1], argv[2], and so on, point to the command-line arguments as they were entered on the command line arguments are separated by whitespace. If a command-line argument needs white space, the user surrounds the phrase with quote marks("), assuming your operating enviroment supports such command-line expansion. For example, consider the following command-line, which includes the program name that you type to run the program :pr13002 foo bar "foo bar"The parameters point to the following null-terminated strings as shown in table 13-1 { argv[0] pr1301{ argv[1] foo{ argv[2] bar{ argv[3] foo bar<pre class='_prettyXprint _lang-auto _linenums:0'>#include <iostream>
using namespace std;
int main()
{
cout << "This program is " << argv[0] << endl;
for(int arg = 1; arg < argc; arg++)
cout << "Argument " << arg << ": " << argv[arg] << endl;
return 0;
}</pre>this program displays the command-line arguments on standard output by iterating the argv array through the element subscripted by the argc parameter. if you enter the command lines shown above and run the program from within the compiler. An application program interprets the meaning the meaning of command line arguments and modifies how the program works accordingly. command-line arguments can include program switch settings, modes, filename lists, path specifications, and so on.ok so what argc does is store the number of arguments while argv stores the
characters in a pointer to arrays 0 being the name of the program its self i see
in the above program how to print the contents of of argv but later on it says that it
could be used for switch settings i know how it could store the path specifications and
lists but switch settings im lost and wouldn't some one have to be familiar with the
program to start with in order to put in the right arguments to pass to the program
READ: the above writtn code was taken from teach yourself c++ by al stevens
and released here by the gnu license agreement
Edited by Avatar/Oroborus, 11 March 2007 - 12:26 PM.