Xref: utzoo comp.bugs.4bsd:1666 comp.std.c:4180 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!wuarchive!hsdndev!cmcl2!kramden.acf.nyu.edu!brnstnd From: brnstnd@kramden.acf.nyu.edu (Dan Bernstein) Newsgroups: comp.bugs.4bsd,comp.std.c Subject: Re: Bug in users command Message-ID: <17799:Jan2414:53:2291@kramden.acf.nyu.edu> Date: 24 Jan 91 14:53:22 GMT References: <18981@rpp386.cactus.org> <12360:Jan2320:15:5291@kramden.acf.nyu.edu> <1991Jan24.004044.13362@craycos.com> Organization: IR Lines: 36 In article <1991Jan24.004044.13362@craycos.com> pmk@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