What Can't C Do? (struct & string compares)
tim at unc.UUCP
tim at unc.UUCP
Fri Mar 16 03:24:18 AEST 1984
There are some tricky issues involved in structure comparison, and there is
no single best way to do it for all applications. The difficulty comes in
when you have pointers as attributes (sorry, "members") of structures. You
can do a standard quasi-numerical comparison on them, but that means that
two pointers to different but equivalent objects will be considered unequal,
which is not always what you want. On the other hand, if you descend into
pointers, you will probably need to do it by user-invisible functions, which
any true C hacker would gag at, and you run the risk of hitting a cycle in
the structure and recursing until the stack overflows.
The first method seems to be the only one tolerable; my question is, how
usefiul would it be? Obviously, it would be good for complex numbers and a
few other applications. What does everyone else think?
Now about string comparison. After I suggested this, someone accused me of
being insensitive to the perils of creeping featurism. I assure you that I
am well aware of that problem; otherwise, I would have had a lot more
suggestions. However, I don't think that extending comparison operators to
apply to an existing data type, for which there is already a common ordering
function known and often used, is really "adding" something -- it is just
making the comparison operators more orthogonal. I would also support doing
this in a more general way by allowing array comparisons when the array is
of a comparable data type; the compiler can frequently generate more
efficient code for this than the user easily can anyway. The big problem
with all this is that there is really no such thing as arrays in C. There
are pointers, but comparison is already defined for them. A smaller problem
is that there are two types of (non)arrays which we'd want to compare:
fixed-length and null-terminated. Can anyone come up with a clean solution
for these problems? I would rather have just the string comparison than no
extension of comparison at all, in any case.
--
Tim Maroney, The Censored Hacker
mcnc!unc!tim (USENET), tim.unc at csnet-relay (ARPA)
All opinions expressed herein are completely my own, so don't go assuming
that anyone else at UNC feels the same way.
More information about the Comp.lang.c
mailing list