0

K&R says the following about command line arguments:

The first [command line argument] conventionally called argc (for argument count) is the number of command-line arguments the program was invoked with; the second (argv, for argument vector) is a pointer to an array of character strings that contain the arguments, one per string.

As shown above, the second argument is described as a pointer to an array of strings. Just to clarify, is this saying that the second argument is a pointer to a single array that has multiple strings stored in it?

Wouldn't the syntax just be: main (int argc, char argv)

but the syntax is main (int argc, char *argv[])

The parameter syntax looks more like an array of pointers, where each element points to string constants or the first elements of strings in another array.

The loop construct to print what these pointers point at look more like what I just described:

for(i = 1; i < argc; i++)
    printf("%s\n", argv[i])

Is the argument an array of pointers? If so, What do the elements actually point to? If it's not, what is the parameter?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Spellbinder2050
  • 644
  • 3
  • 13
  • 2
    K&R's terminology is loose; `argv` is a pointer to **the first element of** an array of strings. – Oliver Charlesworth Sep 08 '14 at 22:19
  • 1
    Your `for` loop is skipping the 0-th element. – Alex Reynolds Sep 08 '14 at 22:19
  • Oli, then how could each element point to a different string? Isn't an array of strings just a bunch of string of characters terminated by `'\0'` in one array? – Spellbinder2050 Sep 08 '14 at 22:22
  • @Spellbinder2050: yes, that's what a string is. So we can make the terminology even more precise: `argv` is a pointer to the first element of an array of pointers to `char`; each `char` is the first element in a null-terminated sequence of characters. – Oliver Charlesworth Sep 08 '14 at 22:24
  • Correction: then how could each element in said array be the start value of a separate string? Moving one element up shouldn't give us a new string. – Spellbinder2050 Sep 08 '14 at 22:27
  • It isn't the start value, it's the start *address*. – Oliver Charlesworth Sep 08 '14 at 22:28
  • OK so each of the pointers in `argv` point to arrays of strings? If yes, is each string in a separate array, or is a single or multi-dimensional array? – Spellbinder2050 Sep 08 '14 at 22:37
  • No, each pointer points to a single string. – Oliver Charlesworth Sep 08 '14 at 22:40
  • 1
    Next time you want more info on a declaration, try [C gibberish ↔ English](http://cdecl.org). `char *argv[]` --> "declare argv as array of pointer to char" – chux - Reinstate Monica Sep 08 '14 at 22:56
  • Note that the interpolated annotation ('[command line argument]') is inaccurate. The description is along the lines of 'When a program wants to process command line arguments, it is defined with two arguments. The first …'. The `main()` function is unusual because the standard says it can be written as `int main(void)` or `int main(int argc, char *argv[])`, where the names `argc` and `argv` are conventional but not mandatory, and the types may be equivalents (so `int` could be replaced by a `typedef` name, and `char **argv` is a common way of writing the second argument to the function). – Jonathan Leffler Sep 09 '14 at 06:00

1 Answers1

1

In C, a "string" is actually char[].

Also, array (of any base type) in function signature is fully equivalent to pointer to the first element.

Each argument is a string, i.e. a char[], or char*.

An array of strings is char *[], or char**.

Therefore K&R are right, and the signature is what it should be.

BTW, argv[0] is typically the file name of the program and is not interesting unless you have multiple links to the same executable and are doing different things depending on the name by which your program was called. You can also use it for user feedback, such as usage instructions.

ArunasR
  • 1,907
  • 1
  • 14
  • 15
  • Being really pedantic here (although it seems appropriate in this context): A *string* is a series of characters ending in a null character. An array of `char` is a region of storage which may or may not contain a string. – M.M Sep 09 '14 at 06:08
  • Also: in a function parameter list, `char *argv[]` [does not denote an array](http://stackoverflow.com/questions/22677415/why-do-c-and-c-compilers-allow-array-lengths-in-function-signatures-when-they), it actually means `char **argv`. In all other declarations, `[]` does actually denote an array (which is different to a pointer). In general, a pointer may or may not point to an array element. (The `argv` pointers are defined to actually point to array elements). – M.M Sep 09 '14 at 06:10