More on pointers/arrays: Function Arguments
Morris Keesan
keesan at bbncca.ARPA
Thu Apr 12 06:30:56 AEST 1984
------------------------------
Dave Brownell (sequoia!brownell) gives an example of code of the sort
foo(s)
char s[];
{
. . .
++s; /*AARGH*/
}
and objects to the ++s because "arrays are not lvalues" and ++ is explicitly
defined as "++ lvalue". The given code is actually correct, although poor
style, because of the following, from the last paragraph of section 10.1 of the
C Reference Manual (p. 205 of K&R)
Also, since a reference to an array in any context (in particular as an
actual parameter) is taken to mean a pointer to the first element of the
array, declarations of formal parameters declared "array of ..." are
adjusted to read "pointer to ...".
Somewhat more clearly put, perhaps, though not part of the language definition,
is the sentence on page 95 of K&R which says "As formal parameters in a
function definition, char s[]; and char *s; are exactly equivalent;"
What this means is that in the context of the function foo(), above, s is
not an array, it is a pointer, and is thus an lvalue. This one case of the
equivalence of s[] and *s is also the source of the recently discussed
confusion when people try to use them equivalently other than in parameter
declarations.
--
Morris M. Keesan
{decvax,linus,wjh12,ima}!bbncca!keesan
keesan @ BBN-UNIX.ARPA
More information about the Comp.lang.c
mailing list