Command line arguments are used when you run a program from the command line, typically when you see a
C:\
and enter something like HeloWorld hello world to get
C:\HelloWorld hello world
That would mean you wat run a program called HelloWorld which is located on the C drive and you want to pass it the arguments of hello and world. In that case argc would be 3, meaning there are 3 items, HelloWorld, hello and world on the command line. HelloWorld, hello and world are located argv. argv is used as an array of C style strings, meaning you can access them just like you would an element of any other array.
In addition to sending arguments to use in the program you can also set a variety of flags that can be used by the compiler/linker instead of doing
Many people use an IDE and sledom use the command line. Others prefer to use the command line and seldom use an IDE. Use whichever you like.
In addition to sending arguments to use in the program using the command line you can also set a variety of flags that can be used by the compiler/linker instead of doing so in the IDE.
Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 8
Further,
The array of strings (argv) is (essentially) created by the operating system at the time the program is invoked. The argc value is how many space delimited items were on the command line (including the program name) and thus how many valid strings are in the argv array.
You should never try to access a string beyond argc-1. If you do, all I can say is "Beyond here be dragons".
Also, you should not modify the strings in the argv array. You don't know how big each string's array is, each could be allocated to be just big enough to hold its content, or they might each be the size of the largest needed. Just leave them alone - copy the argv values to strings you've allocated if you need to modify their contents.
While a great many C and C++ textbooks don't even mention command line arguments, the topic is still relevant. You need to understand how they work if you want to write a Windows program that works by drag and drop or double-clicking of the document files.
vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6
In C and C++ type char is different from type std::string or a C style string.
In this case the asterix indicates a pointer to type char. The whole business of chars, strings, pointers, etc, can be quite difficult to keep track of. In fact, I won't guarantee the interpretations indicated below, though I'm pretty confident of them.
char c; //individual char
char A[]; //array of type char, if null terminated this becomes a C style string.
char A[][]; //array of arrays of type char, each array may or may not be a C style string.
char * c; //a pointer to type char, may be used a char array under some circumstances, for example if assigned a pointer to type char by the new keyword
char *[] c; //an array of pointers to type char, may be used as an array of C style strings under the correct circumstances
char ** c; //a pointer to a pointer to type char, may be used as an array of C style strings under the correct circumstances
Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 8
Question Answered as of 3 Months Ago by
Lerner,
vmanes
and
Tumlee