3

I know it is used to use arguments from the command line, I don't get the declaration though. char* argv[]? does it mean a pointer to a char array, if so why no size? don't you have to have a size if it's not a dynamic array?

I have done a bit of research and come across people saying it decays to char **argv. how does this decay thing work?

Thanks for your help - sorry for being a noob xD

user3247608
  • 583
  • 3
  • 9
  • 17
  • 3
    Use `search` please before posting here. Internet, book, even stackoverflow have answers for this question already. Take a look [at this](http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean) at least. – VP. Jan 29 '14 at 06:53

6 Answers6

4

The best way to look at argv is as an array of pointers to characters/strings. Each argv[i] points to a character which is the start of a null-terminated string.

You are passed argc to tell you how many entries are in the array, but this is technically redundant since argv[argc] == NULL, so you have two ways of detecting the end of the argument list.

The decay 'thing' is that when you pass an array to a function, it is treated as a pointer to the first element of the array.

Thus, if you pass int a[10] to a function, the value is &a[0] and is an int *. In the function declaration or definition, you can write int *x or int x[] interchangeably. The only difference between this and argv is that the element type is not int but char *, so char *argv[] and char **argv are equivalent in the declaration or definition of a function. Note that this equivalence does not apply to either local or global variables. There are major differences between extern char **v; and extern char *a[];.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

It's not uncommon to pass an array with a pointer to the array and its size. If you needed the size to pass the pointer, then this would be impossible to do. The size is only needed to allocate the array anyway.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

You have the size; argc. Though you can use that syntax in a function signature, you can't truly pass arrays to functions in C++. It's a char**.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

Command-line arguments are passed as an array. Arrays in C and C++ are just a contiguous block of memory, they do not come with bounds-checking or a declared size (as they do in Java .length and .NET .Length).

argv is a pointer to an array of strings. This is char** or char* argv[] depending on what syntax you use. argc is the size of the array.

Because pointers in C/C++ are always a fixed size, the size of the array is always argc elements, or sizeof(void*) * argc bytes.

An array of (basic) strings in C/C++ is actually char** because it's an array of pointers to the first character of a null-terminated string, like so:

char** stringArray = &{
    0x1234,
    0x4567,
    0xABCD,
}
0x1234 = &"Some string\0";
0x4567 = &"Another string\0";
0xABCD = &"Third string\0";
Dai
  • 141,631
  • 28
  • 261
  • 374
1

"does it mean a pointer to a char array ... ?"

No. char* argv[]; declares an array of strings (an array of pointers of type char *)

"why no size?"

Because the size may vary, it is not fixed ~ note that this type of parameter ((char*)[]) is equivalent to char** since arrays decay into pointers (which is the main reason why the size of an array cannot be retrieved by just doing something like sizeof(argv)/sizeof(char*) - argv is a pointer). That's the reason why count of arguments (count of the elements in argv) is being passed to main in form of argc as well.

LihO
  • 41,190
  • 11
  • 99
  • 167
  • To be picky (sorta), you can't pass an array to a function. You can use that syntax if you like, but it's still a `char**`. Even `char *argv[10]` would happily allow an array of different size to be passed to the function. – Ed S. Jan 29 '14 at 06:54
  • @EdS.: Decaying... I know, I know... :D – LihO Jan 29 '14 at 06:55
  • I figure you know, but there are so many questions from beginners about this I thought it would be good to clarify – Ed S. Jan 29 '14 at 06:56
  • Heh, yeah. I always end up feeling like a pedantic jerk in threads about C or C++ arrays – Ed S. Jan 29 '14 at 07:01
1

It is an array containing pointers to characters.

Thus, argv[0] is actually a pointer to an area of memory containing the characters of the first argument (actually the command name).

In C, arrays are not bound checked - there is no implicit size of the array. In the case of argv, you can tell the number of arguments via argc.

To find the length of the individual arguments in each element of argv, you can use the fact that character strings are terminated with null characters.

The statement that it 'decays to char **argv' refers to the near equivalence of arrays and pointers in C. In most regards, you can treat an array variable as a pointer to the first element in the array.

There are some important differences between arrays and pointers, though, so some caution is required. See for example this article for some more depth.

harmic
  • 28,606
  • 5
  • 67
  • 91
  • I feel like a tool being so picky in the comments here, but... *"Thus, argv[0] is actually a pointer to an array of characters"* - it's not. It's a pointer to char (`char*`), not a pointer to array of char (`char (*)[]`) – Ed S. Jan 29 '14 at 06:59
  • Thanks @EdS. Updated accordingly – harmic Jan 29 '14 at 07:05