-1

I would like to know why argv array can hold multi-character elements in the following code:

#include <iostream>

int main(int argc, char *argv[])
{
  for (int nArg = 0; nArg < argc; nArg++)
  {
    cout << nArg << " " << argv[nArg] << endl;
  }

  return 0;
}

The code just prints out all arguments passed to the program.

When I run the program with arguments foo, and bar, argv[1] is foo and argv[2] is bar.

But argv is an array of char. And foo is not char, but a string. How can argv[0] be foo?

mc9
  • 6,121
  • 13
  • 49
  • 87

4 Answers4

2

argv is actually a pointer, not an array. In both C and C++, a function parameter cannot be an array. If it looks like an array, it's quietly "adjusted" at compile time to be a pointer to the array's element type.

This:

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

really means this:

int main(int argc, char **argv)

When your program is invoked, argv will be initialized to point to a char* object. That object is the initial element of an array of char* pointers, each of which points to (the initial element of) a string -- except that the final element, argv[argc] contains a null pointer.

argv[0] is a pointer to a string that represents the name of the program. If the program is invoked with two arguments, argv[1] and argv[2] will point to those arguments.

Finally, if you print a char* value using std::cout << ..., it will print, not the pointer value itself, but the value of the string that it points to.

When I run the program with arguments foo, and bar, argv[0] is foo and argv[1] is bar.

Are you sure about that? (Update: That was a typo in the question, now corrected.) If your program is named "my_program", the output of my_program foo bar should be something similar to:

0 my_program
1 foo
2 bar
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • That makes sense now. Thanks. As for the value of `argv[0]` and `argv[1]`, you are right-I had a typo in my question. Editing now. – mc9 Oct 01 '15 at 23:32
1

But argv is an array of char.

Correction: it's an array of char *. "foo" is a char *, so this works.

An array of char is char argv[].
An array of char * is char *argv[]. That's what you have.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    Correction: `argv` is a pointer to a pointer to `char`. There are no array parameters in C or in C++; a declaration that looks like an array parameter is "adjusted" so it's really a pointer to the element type. `char *argv[]` really means `char **argv`. (There's a run-time requirement that the `char*` object that `argv` points to is the initial element of an array of `char*` objects, each of which points to a string.) – Keith Thompson Oct 01 '15 at 23:12
1

argv[0] is foo and argv[1] is bar ...

You should note that argv[0] always receives the actual program name called from the shell, so your command line arguments start from argv[1].

You're just off by one.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

But argv is an array of char.

It's not. It's an array of char pointers, and those pointers point to (zero-terminated) char arrays, i.e. C-style strings.

Technically, argv is not an array, but a pointer to an array (the array containing the char pointers). See Keith Thompson's comment on John Kugelman's answer for more details.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157