main(int argc, char* argv[]) dont understand

354 views Asked by At

i am trying to understand int main( argc, char* argv[]) thing. When i using arguments like ./program 1 bbbbbb code:

cout<< argv[0] << ' ' << argv[1] << ' '<<  argv[2] << endl; 

will show: ./program 1 bbbbbb

but in this case:

 cout<< *argv[0] << ' ' << *argv[1] << ' '<<  *argv[2] << endl; 

will show: . 1 b

SO my question is. Is this char* argv[] in this situation an array of pointers. And how to get access for strings like bbbbbbb.

Ty in advance!

3

There are 3 answers

5
Shoe On

Is this char* argv[] in this situation an array of pointers.

In this case char* argv[] is an array of null terminated strings.

Specifically as per ยง5.1.2.2.1/2 of the C standard:

If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup.

And:

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.


And how to get access for strings like bbbbbbb.

My suggestion is to do what C++ unfortunately fails to do due to backward compatibility:

std::vector<std::string> args(argc);
std::copy(argv, argv + argc, args.begin());

and here's the live demo.

2
brokenfoot On

how to get access for strings like bbbbbbb.

You have answered your own question when you say :

cout<< argv[0] << ' ' << argv[1] << ' '<< argv[2] << endl;
will show: ./program 1 bbbbbb

char* argv[] is an array of pointers and contains the command line arguments (including the excutable name) and argc contains the count. from the example above,

argv[0] = ./program
argv[1] = 1
argv[2] = bbbb  

and

argc=3

Read this for more clarity: What does int argc, char *argv[] mean?

4
user1689571 On

char *[] is read as an array of pointers to char. Generally, a pointer to char represents a a pointer to a null terminated string. This is by convention, and when you pass a char * to a routine, like cout, it will print all the characters that the char * points to until it reaches a null.

So, argv[0] points to a memory address starting with the char '.', which has after it more characters until it reaches the null termination: "./program\0". The cout routine which handles char * will print each character until the null.

When you "dereference" a pointer to char, as in *argv[0], you are asking to return the item that the argv[0] points to. In this case, * (char *) is saying return to me the character that is being pointed to by the (char *). The type of this item is a char, aka a single character. The cout routine which prints char (and not char * this time) is instead called, and it prints just that character that is being pointed to. In the above case, that is a '.'.