Complaint with C standard - (nf)

darryl at ism780.UUCP darryl at ism780.UUCP
Fri Jul 13 10:08:02 AEST 1984


#N:ism780:12500011:000:1727
ism780!darryl    Jul 11 10:57:00 1984

[]

I'm not quite sure who to complain to, but if I post this here someone
on the ANSI C Language committee will perhaps see this.

I have been looking at the Feb. 27, 1984 draft for the C language, and I
want to pick a nit with the wording in section 4.4, additive operators:

"Two pointers to members of the same array may be subtracted.  The result
is converted (by dividing by the size of the member) to a signed integer
(the size of which is implementation-dependent) representing the number
of objects separating the pointed-to objects.  As with any other arithmetic
oveflow, if it will not fit in the space provided, the result is undefined."

If you look at K&R, section 7.4, you will find very similar wording.  Both
paragraphs are poorly worded, or else pcc flagrantly violates this wording.

For the sake of example, assume two pointers, p1 and p2, which are pointers
to objects of the same (arbitrary) type.  Then, according to the wording,
p1 - p2 should have the same value as p2 - p1, because the result of the
subtraction is the number of members separating *p1 from *p2.  Also note
that, if p1 = &ary[n] and p2 = &ary[n+1], there are no members separating
*p1 from *p2.  Following this, &ary[n]-&ary[n+1] == &ary[n]-&ary[n] == 0!!!

The new standard specifically says that the result of pointer subtration
has implementation dependent size.  This will break programs that used
to know that it was an int, and pass the result of such a subtraction
to a function expecting an int.

The former is a longstanding problem with K&R; let's not propogate it into
the new standard.  The later probably not too terrible and does solve other,
more pressing problems.

	    --Darryl Richman
	    ...!cca!ima!ism780!darryl



More information about the Comp.lang.c mailing list