[comp.lang.c] Structure type mismatch

edw@ius2.cs.cmu.edu (Eddie Wyatt) (04/10/87)

In article <6821@brl-adm.ARPA>, dsill@NSWC-OAS.arpa writes:
> Rich Chomiczewski, AT&T - ERC, Princeton NJ <erc3ba!rsc> wrote:
> 
> >Given the following:
> 
> >struct {
> >	int a,b;
> >} foo()
> >{
> >	struct {
> >		int a,b;
> >	} c;
> >
> >	return c;
> >}
> 
> Compiler complains about type mismatch.  This is because the two structures,
> even though they are equivalent, are not of the same type. Try this instead:
> 
	I always thought C used structure equivalence and not name 
equivalence.   In the example, both the return argument and declared
function are structurally equivalent, right?

P.S. I'll get back to FP argument when I have time.  I in the midst debugging
an asycronous processing  problems.
-- 
					Eddie Wyatt

They say there are strangers, who threaten us
In our immigrants and infidels
They say there is strangeness, too dangerous
In our theatres and bookstore shelves
Those who know what's best for us-
Must rise and save us from ourselves

Quick to judge ... Quick to anger ... Slow to understand...
Ignorance and prejudice and fear [all] Walk hand in hand.
					- RUSH 

rbutterworth@watmath.UUCP (Ray Butterworth) (04/12/87)

In article <6821@brl-adm.ARPA>, dsill@NSWC-OAS.arpa writes:
> Try this instead:
> struct { int a,b; } structtype;
> struct structtype foo() { struct structtype c; return c; }

Better yet, try this:
    #include "foo.h"
    FooType foo()
    {
        FooType c;
        return c;
    }
where "foo.h" contains:
    typedef struct {
        int a;
        int b;
    } FooType;
    extern FooType foo();
Then the function that calls foo() can include foo.h too and not
have to define the structure a third time.  It also means that if
you want to add an extra field to the foo structure, you need only
make the change in one very obvous place.  Putting the extern into
the header file means that you don't have to declare the function
in every file that uses it, and that if you should declare it
incorrectly somewhere, the compiler will let you know about it.

Another point of style I've learned to avoid is the use of
commas in declarations.  e.g. instead of "int a,b;" I always
use "int a; int b;".  In the case of int it doesn't really matter,
but if it were "struct foo *a,*b;" and at some time in the future we
decided to get rid of struct foo* and replace it with TypeFooPtr, it
makes it a lot easier when changing the hundreds of declarations of
"struct[  ][  ]*foo[  ]*\*[  ]" to "TypeFooPtr ".  (The brackets
contain blank and tab.)  With the comma list you have to do this
by hand.