The type of argv in K&R2

C

candide

Hi,

K&R2 §5.10 tells that the second parameter to the main function,
usually called argv, has type pointer to character strings.

Some quotes from the book :


-------------------- 8< ----------------------------------------
"the second (argv, for argument vector) is a pointer to an array of
character strings"

"Since argv is a pointer to an array of pointers,"

"Since argv is a pointer to the beginning of the array of argument strings"
-------------------- >8 ----------------------------------------


In fact, argv is rather an array of strings, as explained in the
preceding § :


-------------------- 8< ----------------------------------------
"Compare the declaration and picture for an array of pointers:
char *name[] = { "Illegal month", "Jan", "Feb", "Mar" };"
-------------------- >8 ----------------------------------------


Right ?

Thanks
 
D

Daniel Pitts

candide said:
Hi,

K&R2 §5.10 tells that the second parameter to the main function,
usually called argv, has type pointer to character strings.

Some quotes from the book :


-------------------- 8< ----------------------------------------
"the second (argv, for argument vector) is a pointer to an array of
character strings"

"Since argv is a pointer to an array of pointers,"

"Since argv is a pointer to the beginning of the array of argument strings"
-------------------- >8 ----------------------------------------


In fact, argv is rather an array of strings, as explained in the
preceding § :


-------------------- 8< ----------------------------------------
"Compare the declaration and picture for an array of pointers:
char *name[] = { "Illegal month", "Jan", "Feb", "Mar" };"
-------------------- >8 ----------------------------------------


Right ?

Thanks
All of those are fundamentally the same type, the only real difference
is the semantics that the programmer considers (if any at all).

It is common to declare it as "char **argv", which is a "pointer to char
*" where char * is "pointer to a char".

The semantics of it that make sense is "a pointer to the first element
in an array of char *" where the char * represents "a pointer to the
first char in a null terminated string" (also called a string).

Hope this helps,
Daniel.
 
B

Ben Bacarisse

candide said:
K&R2 §5.10 tells that the second parameter to the main function,
usually called argv, has type pointer to character strings.

Some quotes from the book :

-------------------- 8< ----------------------------------------
"the second (argv, for argument vector) is a pointer to an array of
character strings"

"Since argv is a pointer to an array of pointers,"

"Since argv is a pointer to the beginning of the array of argument strings"
-------------------- >8 ----------------------------------------

In fact, argv is rather an array of strings, as explained in the
preceding § :

argv can't be an array. It is a function parameter and arrays can't
be passed to functions in C. There is indeed an array "out there" but
all main can ever see is a pointer to it.

Not everyone likes the term "array of strings" -- it can be see as a
bit woolly. What is not in question (I hope) is that, inside main,
argv is not an array: it is a pointer to the first element of an array
whose elements are character pointers. When main is called, a further
guarantee is made: that these pointers (if they are not NULL) point to
strings (i.e. that the character arrays pointed to are
null-terminated).

You will hear people say "argv is an array of strings" and that is fine
at the bus stop (if you frequent that sort of bus stop) but only
because everyone know what it really meant: that the second parameter
passed to main is a pointer to an array of pointers that point to strings.
 
B

Barry Schwarz

Joe said:
... snip ...

Nope. It is most correctly 'char **argv', pointer to pointer to
char. Declaring 'char *argv[]' is exactly equivalent. It says
argv is an array of pointers to char. Expressing argv as an array
yields a pointer to its first element.

I believe 'array of strings' is misleading. It is 'array of
pointer to char' in fact.

However those pointers have been initialized to point to arrays of
char, and each array of char has been filled with a '\0' terminated
string. The first pointer (the 0th) points to a string describing
the running program iff that information is available else an empty
string, and the argcth points to a NULL pointer, marking the end of
the list.

Of course you meant the argcth *is* a NULL pointer.
Note the difference between a pointer to an empty string (no data)
and a NULL, which marks the list end.


Remove del for email
 
C

candide

Richard Heathfield a ecrit :
candide said:


I don't think it says so in exactly those words

Oops, i should have said :

"has type pointer to array of character strings."
 
C

candide

Ben Bacarisse a ecrit :
argv can't be an array.


In K&R, the argv parameter is declared as :

char *argv[]


so the type of argv is array to pointer to char. And the C90 standard
agrees :

----------------------- 8< ---------------------------------
5.1.2.2.1 Program startup

(...)the array members argv[0] through argv[argc-1] (...)
(...)to by the argv array shall be (...)
----------------------- >8 ---------------------------------

K&R declares argv to be "a pointer to an array of pointers" and it seems
to me to be incorrect.
 
C

candide

Richard Heathfield a ecrit :
But that doesn't mention type at all, does it?

So what does it mention ? What the "pointer to an array of character
strings" type is referring to ?
> The type of argv is char **.

Which rules do you follow to find the type a function parameter is ? The
C90 standard says :

---------------------- 8< ----------------------------------
6.5.4.3 Function declarators (including prototypes)
(...)
A parameter type list specifies the types of, and may declare
identifiers for, the parameters of the function.
---------------------- >8 ----------------------------------
 
C

candide

Richard Heathfield a ecrit :
It mentions what argv /is/ (albeit rather imprecisely),


Good news! the Standard answers ontological question!!! ;)
>
> It mentions what argv /is/ (albeit rather imprecisely), not what
>/type/ it has.

Paraphrasing the standard, char *argv[] declares the identifier argv and
specifies the type of the argv parameter of the function main().

> has. The type is char **.

According to which C rules ?
 
C

candide

Richard Heathfield a ecrit :
3.5.4.3 of C89 says, in part:

For each parameter declared with function or array type, its type for these
comparisons is the one that results from conversion to a pointer type, as
in $3.7.1.


I think this excerpt doesn't answer exactly my question : the word
"comparisons" :

"or each parameter declared with function or array type, its type for
these _comparisons_ "

refers to the condition for two functions to be compatible and this was
not the point.

3.7.1 of C89 says, in part:

A declaration of a parameter as ``array of type '' shall be adjusted to
``pointer to type ,''


"adjusted" ? The wording is a bit curious. Nevertheless, I did'nt pay
much attention to this fragment, so thanks for the reference.


Thus, the declaration char *argv[], which would otherwise be "array of
pointer to char", is adjusted to "pointer to pointer to char".

OK, thanks.


[I realize this question appears regularly in comp.std.c.]
 
K

Keith Thompson

Richard Heathfield said:
candide said: [...]
(...)to by the argv array shall be (...)

That isn't fine - it's sloppy wording. But we've been round this loop
before, and the committee aren't about to change anything.
[...]

I agree that it's sloppy, but a friendly reading (one that makes it
correct) is possible.

If you interpret the phrase "the argv array" to mean "the array to
whose first element argv points", then the wording is correct. Note
that this does *not* imply that argv is itself an array.

But it's perfectly reasonable to intepret the phrase "the argv array"
to mean "the array named ``argv''", and there is no such array
(there's only a pointer named ``argv''). So yes, the wording is
sloppy.
 
K

Keith Thompson

candide said:
Richard Heathfield a écrit :


The exact sentence is :

"the second (argv, for argument vector) is a pointer to an array of
character strings that contain the arguments, one per string."
[...]

Assuming that's a correct excerpt from K&R2, I'd argue that it's
inaccurate, or at the very least imprecise.

There's arguably no such thing as an "array of character strings". A
"string" is "a contiguous sequence of characters terminated by and
including the first null character" (C99 7.1.1p1; I believe it's
similar or identical in C90). It's a data *format*, not a data
*type*.

If you had an array of character arrays, where each character array
contains a string:

char arr[4][4] = {
"aaa",
"bbb",
"ccc",
"ddd" };

then you might reasonably call this an "array of strings" -- but
that's not what argv points to. argv points to the first element of
an array of pointers, where each pointer (except the last, which is
null) is a pointer to a string. And we can use the phrase "pointer to
a string" only because the standard specifically defines it as "a
pointer to its initial (lowest addressed) character" (C99 7.1.1p1).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top