0

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!

user3402584
  • 410
  • 4
  • 8
  • 18

3 Answers3

2

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.

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • so why it is (*) placed? – user3402584 Mar 17 '14 at 17:07
  • 1
    @user3402584, what do you mean? – Shoe Mar 17 '14 at 17:08
  • i mean, if argv containts pointers that mean it contains addresses.So in order to check what this pointers are pointing exactly, i should use (*) to get value. So for instance i use *(argv[2]) to get bbbbbb but i get only b. – user3402584 Mar 17 '14 at 17:13
  • @user3402584, `char` arrays are *special* in C. They are intended to be null terminated. Just forget about it and use the two lines code I've posted at the end of this answer and you'll be fine. – Shoe Mar 17 '14 at 17:14
  • 2
    @user3402584: Yes, because `char*` is a pointer to `char`s. So if you dereference the pointer, you get one `char`. ;) – Lightness Races in Orbit Mar 17 '14 at 17:36
1

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?

Community
  • 1
  • 1
brokenfoot
  • 11,083
  • 10
  • 59
  • 80
  • ty, i will read it. But if if argv is an array of pointers, shouldnt it contains an various addresses? – user3402584 Mar 17 '14 at 17:15
  • This is what a character pointer is. String declared such a way points to the location where the null terminated string is located. because C++ hides the format specifier in `cout`, if it was `c`, you'd have something like this - `char name[]="siva"; printf("%s\n",name);`. It's just the equivalent. – brokenfoot Mar 17 '14 at 17:21
1

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 '.'.

user1689571
  • 495
  • 1
  • 4
  • 17
  • what does null terminated means?that at the end it has '\0' ? – user3402584 Mar 17 '14 at 17:19
  • i am really grateful for help! – user3402584 Mar 17 '14 at 17:27
  • yes, each character in a string is actually stored as a number. 'A' is 41, 'B' is 42, etc. Null, or '\0' or just 0 is how C strings are terminated, so routines know when to stop (as opposed to Pascal, which would have the length of the string at the beginning). But you can't just put a 0 at the end of your string, because it would be confused with the character '0' (which has the value of 30, similar to how the 'A' has a value of 41). So C provides the escape character '\' which you can place in front of the 0 to denote it as the value 0 and not the character 0 – user1689571 Mar 18 '14 at 00:09
  • ...The '\0' is added automatically when you use double quotes, e.g. "program". – user1689571 Mar 18 '14 at 00:11