[net.unix-wizards] Need help with C

spw2562@ritcv.UUCP (Fishhook) (01/29/86)

    I'm having some problems with structures in a C program I've been
    writing.  I have a structure declared as follows:

    struct datestruct
      {
	char month, day, year;
      }

    Further on I have a structure declared:

    struct dbstruct
      {
	double ...
	struct datestruct date;
      }

    and the declarations:

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

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

==============================================================================
        Steve Wall @ Rochester Institute of Technology
        Usenet: ..!rochester!ritcv!spw2562        (Fishhook)   Unix 4.2 BSD
        BITNET: SPW2562@RITVAXC                   (Snoopy)     VAX/VMS 4.2

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (02/01/86)

>     I'm having some problems with structures in a C program I've been
>     writing.  I have a structure declared as follows:
> 
>     struct datestruct
>       {
> 	char month, day, year;
>       }
> 
>     Further on I have a structure declared:
> 
>     struct dbstruct
>       {
> 	double ...
> 	struct datestruct date;
>       }
> 
>     and the declarations:
> 
>     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.
>     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?

Struct comparison is not supported by C,
just assignment, passing or returning to/from functions,
and member selection.
You will have to compare the three elements individually
(or use memcmp(3C)).

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

net.lang.c (INFO-C)

ark@alice.UucP (Andrew Koenig) (02/01/86)

>    I'm having some problems with structures in a C program I've been
>    writing.  I have a structure declared as follows:

	(and so on)

You can't compare structures.  Sorry about that.

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