incomplete types (was: Recursive #includes)

Michael Condict mnc at m10ux.UUCP
Wed Mar 15 05:58:28 AEST 1989


In article <976 at philmds.UUCP>, leo at philmds.UUCP (Leo de Wit) writes:
- In article <9842 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
- |There is nothing inherently "bad" about incomplete types, especially
- |incomplete structure declarations.  In fact they are essential for
- |declaring structures that contain pointers to each other.
- 
- How essential that is? Consider:
- 
- struct egg {
-      struct hen {
-           struct egg *eggp;
-      } *henp;
- };
- 
- This declares two structure types, each containing a pointer to the
- other one. And I can give more complex examples ...

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

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.
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.  Things like struct tags that are global, should be declared only
globally, i.e. outside of any function or other struct.

By the way, can someone tell me if the above code defines "struct hen" globally
in ANSI-C?
-- 
Michael Condict		{att|allegra}!m10ux!mnc
AT&T Bell Labs		(201)582-5911    MH 3B-416
Murray Hill, NJ



More information about the Comp.lang.c mailing list