Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!att!tut.cis.ohio-state.edu!ucbvax!UCBVAX.BERKELEY.EDU!"Dan Karron From: Dan Karron@UCBVAX.BERKELEY.EDU Newsgroups: comp.sys.sgi Subject: Re: struck by typedef struct... Message-ID: <9012151623.AA06535@karron.med.nyu.edu> Date: 15 Dec 90 16:23:49 GMT Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: karron@cmcl2.nyu.edu Organization: The Internet Lines: 71 >Subject: Re: struck by typedef struct... > >|> typedef struct MyTag { >|> MyStruct a; >|> void (*a_subroutine_transfer_address)(MyStruct *s); >|> } MyStruct; >|> >|> This don't fly. > >I'm no authority on ANSI C (my coworker is), but I see three things wrong >with this: > > 1) The compiler does not recognize MyStruct as a type since it > is not yet defined. > > 2) The compiler does not know what size MyStruct is the first > time you use it. > > 3) The definition of MyStruct is recursive which can't work. > > >|> typedef struct MyTag { >|> struct MyTag a; /* Forward reference is legal. */ >|> void (*a_subroutine_transfer_address)(struct MyTag *); >|> } MyStruct; >|> >|> This does. > >This is slightly better because the compiler understands that "struct MyTag" >is a type, but 2) and 3) are still there. Yes, that bothers me too. But the Good Book Says: Chapter 6.5 Page 139 (ANSI edition) .. This recursive declaration of a node might look chancy, but it's correct. It is illegal for a structure to contain an istance of its self but struct tnode *left; declares left to be a pointer to tnode , not a tnode itself. Sayeth the Good Book in another section Chapter 6.7, Page 146 : It must be emphasized that a typedef declaration does not create a new type in any sense: it merely adds a new name for some existing type.... In effect typedef is like a #define, except that since it is interperted by the compiler, it can cope with textual substitutilns that are beyond the capabilities of a preprocessor. ... > >Perhaps something got lost in the translation here, like you meant the >first field to be a pointer, rather than the struct itself. > You are precisely correct. You get the gold pointed star for astuteness! >-Gary > +-----------------------------------------------------------------------------+ | karron@nyu.edu (E-mail alias that will always find me) | | Fax: 212 340 7190 * Dan Karron, Research Associate | | . . . . . . . . . . . . . . * New York University Medical Center | | 560 First Avenue \*\ Pager <1> (212) 397 9330 | | New York, New York 10016 \**\ <2> 10896 <3> | | (212) 340 5210 \***\_________________________________________ | | Main machine: karron.med.nyu.edu (128.122.135.3) IRIS 85GT | +-----------------------------------------------------------------------------+