Bug in users command

Dan Bernstein brnstnd at kramden.acf.nyu.edu
Fri Jan 25 01:53:22 AEST 1991


In article <1991Jan24.004044.13362 at craycos.com> pmk at craycos.com (Peter Klausler) writes:
> Given the declaration
> 	char (*a)[N];
> the expressions
> 	(char *) a
> 	&((*a)[0])
> 	*a
> are all valid in ANS X3.159-1989, all of type "char *", and all yield pointers
> to the same char object.

The second and third are obviously correct, and I don't see why anyone
would code the first way when the third is so much simpler.

> The key is that arrays rarely remain lvalues as such; they are converted to
> pointer expressions when not an argument to "sizeof" or unary "&"

This has nothing to do with the first example.

> Without this automatic conversion, "x[y]" could not be defined as identical
> to "*(x+y)". Please consult section 3.2.2.1 of the standard for the exact
> specification of the automatic array lvalue conversion,

No, this has nothing to do with the first example. If you just look at
what's in 3.2.2, you would conclude that (char *) a is not valid.

However, as Doug Gwyn just told me: ``You were probably concentrating on
the spec in 3.2.2.3 and overlooked the one in 3.3.4 Semantics, which in
essence states that pointers can be cast freely to other pointer types,
provided that alignment constraints are properly observed.'' (He's
right, of course.)

So it is correct to call strncmp() with arguments cast to (char *),
provided that characters are aligned the same way as character arrays
(which I think is true).

---Dan



More information about the Comp.bugs.4bsd.ucb-fixes mailing list