incomplete types (was: Recursive #includes)

Doug Gwyn gwyn at smoke.BRL.MIL
Mon Mar 13 14:21:06 AEST 1989


In article <1567 at ubu.warwick.UUCP> geoff at cs.warwick.ac.uk (Geoff Rimmer) writes:
>In article <9804 at smoke.BRL.MIL> gwyn at smoke.BRL.MIL (Doug Gwyn ) writes:
>> In article <7488 at june.cs.washington.edu> ka at june.cs.washington.edu (Kenneth Almquist) writes:
>> >I've been told that incomplete types cannot be used in function prototypes.
>> I couldn't find such a prohibition in the pANS.
>> (But maybe it's there and I missed it.)
>gcc will not allow it:
>static void function (struct egg * a);
>struct egg { int number1; float number2; };
>static void function (struct egg * a)
>{
>        printf("In function with [%d,%f]\n",a->number1,a->number2);
>}
>str.c:1: warning: `struct egg' declared inside parameter list
>str.c: In function function:
>str.c:6: conflicting types for `function'
>str.c:1: previous declaration of `function'
>The strange thing is that it doesn't barf on the 
>	struct void function (struct egg *a);
>line, even though the structure is as yet undefined.  The problem is
>when it compares the types of the function's arguments, and sees that
>in the call to the function, the argument is type (struct egg *) and
>in the declaration it is unknown.
>Is this a problem with gcc, or does this happen with all ANSI compilers?

Sometimes I wonder if folks in the UK speak the same language.

The first warning from GCC is correct: that structure tag "egg" has
function prototype scope, and although (I believe) an incomplete type
may be used in the declaration, the type must eventually be completed.
Once the end of the function declaration is reached it is obviously
impossible for that "egg"-tagged structure's type to ever be completed,
since the scope of the tag is gone at that point.

The "conflicting types" and "previous declaration" warnings are not
incorrect: the full type of the first declaration of "function" is
unknown due to the earlier problem, whereas the second declaration
(part of the function definition) has a definite type.  One supposes
that that is a mismatch of declaration types.

I don't understand what you mean by "doesn't barf ... even though the
structure is as yet undefined".  That's essentially what the first
warning was about.  If the whole mess had been preceded by
	struct egg;
then there would have been a structure tag "egg" in scope that would
have been taken as the one for the function parameter.  It would
still be an incomplete type, but one that is in principle possible to
complete later in the translation unit (as indeed occurs at the next
statement).  So far as I have been able to determine, that should be
perfectly valid usage.

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.



More information about the Comp.lang.c mailing list