In code like int
main(int argc, char **argv), I'M trying to understand command line arguments a little better. And what would be the difference between
char ** argv &
char * arg? Thanks.
There is no difference, functionality-wise between
char** argv and
char* argv. It's all a matter of preference.
Command line arguments are used when you run a program from the command line, typically when you see a
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.
Edited by Lerner
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.
Thanks. I thought char was a data type for a one character string. But it's been a while since i played with C++ and I'M just now trying to get back into it. One more thing. How might I create a variable to hold one of the argv elements that a number/string into an int? One last thing, what is the relevence of the
* for the char variable? Thanks again.
Edited by lewashby: missed something
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