Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!texbell!rutgers!dptg!att!cbnewsl!dfp From: dfp@cbnewsl.ATT.COM (david.f.prosser) Newsgroups: comp.lang.c Subject: Re: on the fringe of C syntax/semantics Message-ID: <2172@cbnewsl.ATT.COM> Date: 6 Oct 89 18:21:41 GMT References: <453@usage.csd.unsw.oz> Reply-To: dfp@cbnewsl.ATT.COM (david.f.prosser) Organization: AT&T Bell Laboratories Lines: 41 In article <453@usage.csd.unsw.oz> troy@mr_plod.cbme.unsw.oz writes: >This isn't really on the edge of the language specs.... although I ran into >a question last night which was... somebody wanted to define a pair of >structures which were initialised with pointers to eachother. Fine, except >that one hasn't been defined - no address because no space is allocated, and >the compiler doesn't have the faintest idea what you're on about until later, >when you declare the second structure. The solution was to effect a forward >declaration by using the extern keyword. This causes the problem to be passed >off to the linker, which resolves the external reference - from the same file! >struct a_struct { > void *next; > int value; >}; >struct b_struct { > struct a_struct *next; > int value; >}; >extern struct b_struct struc2; >struct a_struct struc1 = { &struc2, 0 }; >struct b_struct struc2 = { &struc1, 0 }; While this code may be required with certain old compilers, the following is valid ANSI C: struct s1 { struct s2 *s2p; /*...*/ }; struct s2 { struct s1 *s1p; /*...*/ }; struct s1 one; struct s2 two = { &one, /*...*/ }; struct s1 one = { &two, /*...*/ }; The first two lines are from page 64, lines 28 and 29. Note that the use of "void *" in the quoted article is neither necessary nor appropriate. The first declaration of "one" is a tentative definition. The following declaration of "one" is the definition. "extern" is unnecessary. Dave Prosser ...not an official X3J11 answer...