argv
is defined as a pointer rather than as an array because there is no such thing as an array parameter in C.
You can define something that looks like an array parameter, but it's "adjusted" to array type at compile time; for example, these two declarations are exactly equivalent:
int foo(int param[]);
int foo(int param[42]); /* the 42 is quietly ignored */
int foo(int *param); /* this is what the above two declarations really mean */
And the definition of main
can be written either as:
int main(int argc, char *argv[]) { /* ... */ }
or as
int main(int argc, char **argv) { /* ... */ }
The two are exactly equivalent (and the second one, IMHO, more clearly expresses what's actually going on).
Array types are, in a sense, second-class types in C. Code that manipulates array almost always does so via pointers to the elements, performing pointer arithmetic to traverse the elements.
Section 6 of the comp.lang.c FAQ explains the often confusing relationship between arrays and pointers.
(And if you've been told that arrays are "really" pointers, they're not; arrays and pointers are distinct things.)
As for why argv[0]
points to the program name, that's just because it's useful. Some programs print their names in error messages; others may change their behavior depending on the name by which they're invoked. Bundling the program name with the command-line arguments was a fairly arbitrary choice, but it's convenient and it works.
char *
represents a C-style string thenchar *x[]
means thatx
is an array of C-style strings – Scenaristi=0
then try to invoke your command from the shell in different ways e.g../foo bar baz
or/path/to/foo bar baz
and expirement to see if there is any difference in how your environment transmits argv[0] – Scenaristchar *x[]
is a parameter declaration, it means thatx
is a pointer to pointer tochar
. It may or may not point to an element of an array ofchar*
pointers, and thosechar*
pointers may or may not point to C-style strings; the parameter declaration doesn't specify that. – Hunkchar **x
in the declaration. Yes.char *
does not automatically imply a C-style string. Also, it's interesting to notice that the definition of C-style strings does not provide any reliable way of testing whether or not something is one. Not sure if it's in the C standard or POSIX, but I think somewhere is specified that in the special case ofargv
this must be contain an array of C-style strings whose last element has the value(char *)0
in conforming implementations – Scenarist