forward declared structures

gwyn at BRL-VLD.ARPA gwyn at BRL-VLD.ARPA
Fri Jul 27 02:13:44 AEST 1984


From:      Doug Gwyn (VLD/VMB) <gwyn at BRL-VLD.ARPA>

Technically it is an error to declare

struct s *ps;

without having defined what a "struct s" is first.
Because of the forward-reference problem, most C
compilers allow declaring a pointer to a struct
before the struct is defined, so that

struct a {
	struct b *bp;
	};
struct b {
	struct a *ap;
	};

is supported.  They get away with this only because
all pointers-to-struct happen to have the same run-time form.

The declaration of `struct s' inside a function is local
to that function, which is why it is unknown later when
you attempt to use the pointer `ps'.  This is perfectly
correct behavior, which supports information hiding.
To make the meaning of `struct s' globally known, you need
to declare it in a global context.



More information about the Comp.lang.c mailing list