Scope of incomplete types

Doug Gwyn gwyn at smoke.BRL.MIL
Sat Sep 8 01:49:17 AEST 1990


In article <1990Sep5.191221.5118 at charis.UUCP> wmmiller at cup.portal.com writes:
>                struct X* xp1;
>                {
>                   struct X* xp2;
>                   }

Neither of these declares a struct X.  That is a crucial point to
understand in conjunction with the example from 3.5.2.3 that you cited.

>... there doesn't appear to be anything in the Standard to indicate any
>difference in the way the scope rules apply to incomplete types.

That's correct; visibility of an identifier (scope) is not affected by
incompleteness of its type.

>        If the type is to be completed, another declaration of the tag in
>        the same scope (but not in an enclosed block, which declares a new
>        type known only within that block) shall define the content.

Yes, basically one of the interesting things X3J11 figured out at the
first interpretation-phase meeting was that the standard insists that
type information is not propagated outward from a block.  That has all
sorts of subtle ramifications, fortunately not affecting programmers
who use sensible coding style in the first place.

For your example, basically I would say that not declaring what a struct X
really is before declaring pointers to it is a poor practice except in the
case of mutually-referential structures as in the 3.5.2.3 example, and even
there one should as a matter of safety predeclare an incomplete type like:
	struct s2;	/* forces s2 to refer to the one in the current scope */
	struct s1 { struct s2 *s2p; /*...*/ };
	struct s2 { struct s1 *s1p; /*...*/ };



More information about the Comp.std.c mailing list