[net.lang.c] Need help with C

throopw@dg_rtp.UUCP (02/03/86)

>    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.

K&R C doesn't allow composite variables (arrays, structures, like that)
to be used in primitive operations (assignment, comparison, like that).
Now, structure assignment works in many compilers nowadays, but I'm not
sure about comparison.  Our typechecker barfs on this sort of thing, and
so does our compiler.  The compiler says:

        if( x==y )
             ^
    The binary operator "==" works only on integer, floating-point
    and pointer data types.

which is pretty much to the point.  In any event, just what did you want
the comparison to do?  Compare for bit-wise equality?  Including
padding?  What if characters have multiple representations for the
"same" character on your machine?  Maybe component-by-component
equality?  With just what semantics?  And so on and on.

>    I'm almost positive I've done this sort of thing before, and can't
>    seem to find why it won't work.  Anyone have any ideas/suggestions
>    as to why this is?

I assume that when you did it before, the item you were grabbing out of
the array of structures was *not* a composite type, but rather belonged
to integer, floating-point or pointer type.

>    Also, is there a better newsgroup for these type of inquiries?

Yes indeedy, called "net.lang.c".   Followup has been redirected there.
-- 
Wayne Throop at Data General, RTP, NC
<the-known-world>!mcnc!rti-sel!dg_rtp!throopw

rb@ccivax.UUCP (rex ballard) (02/08/86)

In article <124@dg_rtp.UUCP> throopw@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?