0

I'm learning c++ and one thing I can't seem to find a good explanation of is how char* argv[] works. I understand that it is an array of characters, but from what I understand if you had

char myword[] = "Hello";

then myword[1] is "e".

So when using argv[], why is argv[1] the entire first argument and not just the second letter of the program name? There is something here I don't understand but I'm not sure what....

Thanks for any explanations!

Matt
  • 35
  • 7

6 Answers6

1

You seem to miss the point that argv (as in int main(int argc, char* argv[])) is a pointer to pointer to char, not the char.

Some funny stuff about arrays and pointers. Since a pointer can be treated to an array (provided it points to an array) and arrays can be treated as pointers (technical term for this is decay to pointers) an array and pointer can be often used interhargeably.

However, when doing so, one still needs to keep in mind that at certain times, there is a stark difference between array and pointer. One of the most visible (not the only one!) is the difference in their sizes.

For example:

char argv[] = "Test!";
char* pargv = argv;

size_t s_argv = sizeof(argv); // equals to 6 - 5 letters of Test! + \0
size_t s_pargv = sizeof(pargv); // equals to whatver size of pointer you have, i.e. 4 or 8
SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • To be pedantic, `argv` is a pointer. But it does always point to an array of pointers - which in turn always point to arrays of characters. – eerorika Jan 21 '16 at 18:26
  • Aha! That makes sense! Thanks! – Matt Jan 21 '16 at 18:27
  • @user2079303, I do agree with this, and I actually considered phrasing it this way, but than thought 'array' will be better understood by OP. – SergeyA Jan 21 '16 at 18:29
  • @SergeyA I just find it amusing that you correct others that `argv[0]` is not an array but pointer since that's exactly as correct (or incorrect) as saying that `argv` itself is an array. – eerorika Jan 21 '16 at 18:30
  • @user2079303, me too. To alleviate this a bit, I've edited my answer for better clarity. – SergeyA Jan 21 '16 at 18:37
0

Because argv is a pointer to a pointer to chars while myword is just a pointer. myword is of type char* while argv is of type char**.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

Because argv (assuming you mean that one declared as main() parameter) is

char* argv[]
 // ^

and not

char argv[]
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

Argv is basically something like this:

enter image description here

So on the left is the argument itself--what's actually passed as a parameter to main. That's the address of an array of pointers. Each of those points to some place in memory containing the text of the corresponding argument that was passed on the command line. Then, at the end of that array there's guaranteed to be a null pointer.

Note that the actual storage for the individual arguments are at least potentially allocated separately from each other, so their addresses in memory might be arranged fairly randomly (but depending on how things happen to be written, they could also be in a single contiguous block of memory--you simply don't know or and shouldn't care).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    Out of curiosity - why did you rearrange third and second arguments, complicating the picture? – SergeyA Jan 21 '16 at 18:35
  • 4
    @SergeyA: To emphasize the fact that each of those is (at least in theory) separately allocated, and you can't expect them to follow any particular order in memory. – Jerry Coffin Jan 21 '16 at 18:38
  • I see. I like the new arrangments much better, by the way. – SergeyA Jan 21 '16 at 18:41
0

I understand that [argv] is an array of characters

It's not. It's not even an array.

There are no array parameters in C++, see e.g. C++ arrays as function arguments and Passing Arrays to Function in C++. The T[] and T[N] syntaxes, iff used in parameter declarations, actually mean T*.

So, argv is a pointer to an array of pointers. Those pointers point to arrays of characters.

Community
  • 1
  • 1
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
-2

char* argv[] is a double array (or whatever you want to call it), the same as char** argv. The object at argv[0] will be another char*, argv[0][0] will be a char.