on the fringe of C syntax/semantics

david.f.prosser dfp at cbnewsl.ATT.COM
Sat Oct 7 04:21:41 AEST 1989


In article <453 at usage.csd.unsw.oz> troy at 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...



More information about the Comp.lang.c mailing list