structure references in header files

Chris Torek chris at umcp-cs.UUCP
Thu Dec 12 10:05:06 AEST 1985


Ai!  Doug, how could you?  You posted an example that is not even
syntactically correct!

C compilers are obliged by K&R to accept declarations of pointers
to structure types even if those structure types have not yet been
declared.  That is, the following is legal:

	/*
	 * Doubly linked list of node headers
	 */
	struct nodehead {
		struct	nodehead *nh_next;
		struct	nodehead *nh_prev;
		struct	node *nh_front;		/* <1> */
	};

	/*
	 * Singly linked nodes with backpointers to the node header
	 */
	struct node {
		struct	node *n_next;
		struct	nodehead *n_head;	/* <2> */
		.
		.
		.
	};

If you intend to use anonymous structures and typedefs, e.g.,

	typedef struct {
		...
	} NODEHEAD;

you still need a name for at least one of the two structures, as
C compilers are allowed to be `one pass' and cannot infer that a
name is a typedef that has yet to be compiled.  At least one of
the two marked references (<1> and <2>) must be to a `struct foo *',
since at least one will be a forward reference no matter how you
organise the declarations.

I would check my X3J11 draft to see what it says, but someone
`cleaned up' my desk.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list