Xref: utzoo comp.bugs.4bsd:1660 comp.std.c:4172 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!usc!elroy.jpl.nasa.gov!ncar!csn!pikes!aspen.craycos.com!pmk From: pmk@craycos.com (Peter Klausler) Newsgroups: comp.bugs.4bsd,comp.std.c Subject: Re: Bug in users command Message-ID: <1991Jan24.004044.13362@craycos.com> Date: 24 Jan 91 00:40:44 GMT References: <6182:Jan2222:06:3991@kramden.acf.nyu.edu> <18981@rpp386.cactus.org> <12360:Jan2320:15:5291@kramden.acf.nyu.edu> Organization: Cray Computer Corporation Lines: 49 In article <12360:Jan2320:15:5291@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: >The question here is whether a pointer to an array of 10 blobs is the >same as a pointer to blob. I don't think so. They're different pointer types. >> int scmp (void *_a, void *_b) >> { >> char (*a)[UT_NAMESIZE] = _a; >> char (*b)[UT_NAMESIZE] = _b; > >Yes! That's exactly my point. I wouldn't bother defining the variables >for this cast, but scmp() has to do something like your example. It's >wrong if you don't cast back to pointer-to-array-of-char. > >> if that gives you some sense of moral superiority, but you are >> stuck doing >> strncmp ((char *) a, (char *) b, sizeof *a); >> by your logic in that case. > >No! That strncmp() call is wrong, wrong, wrong. It's fine. >The correct call is strncmp(&((*a)[0]),&((*b)[0]),sizeof *a)---or, >equivalently, strncmp(*a,*b,sizeof *a). 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 key is that arrays rarely remain lvalues as such; they are converted to pointer expressions when not an argument to "sizeof" or unary "&" or a character array initializer string constant. 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, or any basic ANSI C text for a more elementary discussion of pointers and arrays in C. -Peter Klausler, compiler group, Cray Computer Corp.