Need help with C (struct comparison)
rex ballard
rb at ccivax.UUCP
Sat Feb 8 09:17:44 AEST 1986
In article <124 at dg_rtp.UUCP> throopw at dg_rtp.UUCP writes:
> struct datestruct date;
> struct dbstruct dbase[63];
>
> I try to do a comparison between these, i.e.
>
> ( date == dbase[loop].date )
>
> but the compiler blows up on this, saying the two are incompatible.
The basic problem is that the 'C' doesn't do composite comparisons.
Suppose you declared datestruct as
struct datestruct {
int yy; /* year */
int mm; /* month */
int dd; /* day */
}
On a 68000 machine you could use bcmp();
But on a VAX, 808X, or PDP-11, byte ordering of words is not Most Significant
to Least Significant, therefore the code would be non-portable.
What you need here is a function to compare two dates;
Of course you need functions to compare other complex types as well.
You could use an object oriented language such as C++ which would be able
to interpret this as a function call to a complex structure type.
Some C compilers are able to do assignment automatically by using
bcopy, because the order will be preserved automatically. However,
this is a relatively new feature, and is not really portable unless
you use an actual call to bcopy(). The same is true with passing
structures and arrays (rather than just pointers) to functions.
What an object oriented language (C++ or Objective C) does for you is to
intercept the '==' and the two types and call the appropriate structure
comparison routine (which may be automatically generated in some languages).
This is one of the reasons the 'sort()' routine has such an unusual
calling sequence.
Of course, one advantage of C is that you don't automatically get
compare routines for every composite structure you want to use.
If you can, go back a few articles in this group and read some
of the comments on these 'Object Oriented' versions of the language.
Perhaps a variation of C++ will become a standard superset of the standard
language. It would be nice to have addition, subtraction, assignment,
concatenation, 'to ascii', print, input, and file I/O of complex types.
How about a capability for specifying which of these operations you
actually plan to use (something many OOL's lack) either at declaration
time or by the linker? Or am I the only one who has ever had to write
stubs for the 'printf/scanf floating point' to make an application fit the
constraints of the machine?
More information about the Comp.lang.c
mailing list