Groovy hepcat Keith Thompson was jivin' on Wed, 16 Jul 2003 00:33:38
GMT in comp.lang.c.
Re: Array indexing's a cool scene! Dig it!
pete said:
Keith said:
If compatibility were not an issue, would you really want both
array[index] and index[array] to be valid? If so, why?
If by *really*, you're asking if I think it's important
that I should be able to write code with index[array],
I'd say, "No, not really".
But, one of the things that I like about both the K&R books
and the C89 standard, is that compared to other books and standards,
they're small. Making that particular construct illegal,
would just be another rule to learn.
If you know the rules about pointers,
but the only thing that you know about the array subscript operator
is that index[array], means *(index + array),
then you know or can construe
everything that there is to know about the array subscript operator.
I see your point, but I disagree.
I see "pointer + integer yields pointer" and "integer + pointer yields
pointer" as two different rules. If the left and right operands are
But they're not two different rules. A + B has always been equal to
B + A, and not just in C, but in mathematics and in real life.
If I hold a pencil in my right hand and an apple in my left hand,
I'm holding an apple and a pencil. OTOH, if I hold an apple in my
right hand and a pencil in my left, I'm holding an apple and a pencil.
It doesn't matter that the apple and the pencil are two completely
different types of objects; when they're added to gether, it doesn't
matter which is on the right and which is on the left; they're still
an apple and a pencil. So it is with pointer addition in C.
of vastly different types, I see no great virtue in commutativity. I
would have preferred "pointer + integer yields pointer" and "integer
plus pointer is illegal". Aside from reducing complexity and
How would that reduce complexity? It may increase complexity
eliminating the useless obfuscation of index[array], this would also
more closely parallel the rules for pointer subtraction: "pointer -
integer yields pointer" and "integer - pointer is illegal". (The
Subtraction is not commutative; not just with pointers and integers,
but any types (even the same type). (A - B) != (B - A) except when B
== A, no matter what their type(s).
Integer - pointer just doesn't make sense. Integer + pointer does,
however.
analogy is weakened by the existence of "pointer - pointer yields
integer"; oh, well.)
If pointer + integer yields pointer, then it makes perfect sense
that pointer - pointer yields integer (and that pointer - integer
yields pointer).
This overloading of the "+" operator is built into the language.
Languages that allow programmers to define their own operator
overloading typically use something that looks like (or is) a function
definition. The ones I'm familiar with don't automatically force
user-defined "+" to be commutative. For example, in Ada, a
declaration like
function "+"(Left: Ptr; Right: Integer) return Ptr;
would not imply a declaration like
function "+"(Left: Integer; Right: Ptr) return Ptr;
If you want both, you have to declare both. (Replace function "+"
with operator+ and turn the syntax inside out for C++.)
These are not really addition operators. They are a type of function
designed to do whatever the programmer writes them to do (within the
possibilities of the language, of course).
On the other hand, if you think of commutativity as being at the very
core of what addition is all about, it certainly makes sense for
"pointer + integer" and "integer + pointer" to mean the same thing --
Right. (We are talking about the inherent functionality of addition,
which is commutative in nature, not the "+" character.)
but then you'd have trouble with some languages' use of an overloaded
"+" operator to denote string catenation.
That's not addition, but concatenation. It uses the "+" character to
represent it, but it's not the same thing. It doesn't belong in this
discussion.
In any case, backward compatibility trumps both our arguments, so you
win.
}
Common sense has alot to do with it too.
--
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?