incomplete types (was: Recursive #includes)

Leo de Wit leo at philmds.UUCP
Sat Mar 18 03:50:37 AEST 1989


In article <884 at m10ux.UUCP> mnc at m10ux.UUCP (Michael Condict) writes:
|In article <976 at philmds.UUCP>, leo at philmds.UUCP (Leo de Wit) writes:

  [] self-contained cross-referencing struct example omitted ...

|Yes, but in the example you show, the scope of the "struct hen" tag declaration
|might be limited to the inside of the "struct egg" declaration, depending
|on your C compiler and the rules of ANSI-C. 

There's no such thing as declaration scoped types (if there is I would
be very interested to know where you got that information); the scope
of struct hen is the same as the scope of struct egg (the same block of
whatever).

|                                             Can you show us how to
|declare these two structs, hen and egg, such that both are guaranteed by the
|rules of ANSI-C and current C compilers to be accessible after the declaration,
|without using incomplete types?

Yes, exactly as it stands. I would be very surprised if (draft) ANSI
conformant compilers couldn't handle this. And the C compilers I've
seen do treat it correctly.

|Even if the above example declares the two tags egg and hen globally, it is
|awkward and confusing to have to arbitrarily declare one inside the other.

Ah, that's the real problem. Perhaps you're having trouble reading this
declaration.  As a matter of fact, I have used such declarations more
than once (although one doesn't need cross-referencing structures every
day), and I never found it awkward nor confusing. The declaration not
being symmetrical is more a style issue.

|It will only lead to very confusing error messages when you try to add another
|mutually recursive struct declaration and don't get the ordering or the nesting
|right.

Something like this?

struct egg {
     struct hen {
          struct chicken {
              struct egg *eggp;
          } *chickenp;
     } *henp;
};

|        Things like struct tags that are global, should be declared only
|globally, i.e. outside of any function or other struct.

Struct tags? The struct _type_ (nl. struct hen) is what we are
discussing (the tag 'henp' is also available being a member of the
struct egg, but that's not the point), and as I pointed out before, has
the same scope as the struct egg.

    Leo.



More information about the Comp.lang.c mailing list