on the fringe of C syntax/semantics

Joe English jeenglis at nunki.usc.edu
Sat Oct 7 05:52:57 AEST 1989


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. 
[...]
>struct a_struct {
>	void *next;
>	int value;
>};
>
>struct b_struct {
>	struct a_struct *next;
>	int value;
>};


Except you probably shouldn't use void *.  I just tried this,
and both gcc and cc (SunOS) accepted it:

struct foo;	/* forward declaration -- unnecessary, though */

struct bar {
	struct foo *foop; /* this is OK. */
};

struct foo {
	struct bar *barp; /* OK, struct bar seen alrerady */
	struct diddle *diddlep; /* OK, struct diddle defined later */
	struct qwerty *qwertyp; /* OK, struct qwerty *never* defined */
};

struct diddle {
	int asdf;
};


Presumably the compiler would complain if I tried to
use a foo::qwertyp before it had seen the definition
of struct qwerty, but the rest worked just fine.

(BTW, is this behaviour specified in the standard?)

--Joe English

  jeenglis at nunki.usc.edu
  



More information about the Comp.lang.c mailing list