Bug in users command

Peter Klausler pmk at craycos.com
Thu Jan 24 11:40:44 AEST 1991


In article <12360:Jan2320:15:5291 at kramden.acf.nyu.edu> brnstnd at 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.



More information about the Comp.std.c mailing list