Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!ucsd!rutgers!cmcl2!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: incomplete types (was: Recursive #includes) Message-ID: <9842@smoke.BRL.MIL> Date: 13 Mar 89 04:21:06 GMT References: <570@marob.MASA.COM> <9727@smoke.BRL.MIL> <964@philmds.UUCP> <3804@xyzzy.UUCP> <7488@june.cs.washington.edu> <9804@smoke.BRL.MIL> <1567@ubu.warwick.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 54 In article <1567@ubu.warwick.UUCP> geoff@cs.warwick.ac.uk (Geoff Rimmer) writes: >In article <9804@smoke.BRL.MIL> gwyn@smoke.BRL.MIL (Doug Gwyn ) writes: >> In article <7488@june.cs.washington.edu> ka@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.