Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: forward references in typedefs Message-ID: <10567@smoke.BRL.MIL> Date: 20 Jul 89 22:56:21 GMT References: <55480@tut.cis.ohio-state.edu> Reply-To: gwyn@brl.arpa (Doug Gwyn) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 40 In article <55480@tut.cis.ohio-state.edu> George M. Jones writes: >The following code compiles (Sun, GCC, Green Hills) just fine as expected: > struct one > { > struct two_t *foo; > } one_t; > struct two > { > struct one_t *bar; > } two_t; If you were to change the member types to point to "struct two" and "struct one" types, it would be correct. The lack of a diagnostic at the end of the translation unit ("struct two_t and struct one_t missing declarations") can be considered a compiler deficiency. C deliberately allows forward struct references in such contexts. It may not have been completely clear in K&R 1st Edition, but it's officially specified in the proposed Standard. >However all the compilers I tried appear to be choaking on the forward >reference to two_t in the following chunk of code > typedef struct one > { > two_t *foo; > } one_t; Sure; "two_t" is neither a C keyword nor a defined type at the point that it is first used. The compiler cannot know that it is eventually going to be a structure type, unless it were to scan ahead (which is contrary to the intent of C). You might consider writing something like: typedef struct two two_t; /* incomplete type */ typedef struct one { two_t *foo; ) one_t; struct two { /* completes the type */ one_t *bar; };