forward declared structures

William W Smith wws at siemens.UUCP
Mon Jul 23 22:54:46 AEST 1984


I have a question on forward declarations of structures in C.  Kernighan and
Ritchie does not answer the question and to me the behavior of the Berkeley
Unix C compiler is without question wrong.  To wit:

struct s *ps;

main() {
    struct s { 
	int i1, i2;
	};
}
foo() {
    ps->i1 = 33;
}

My question is whether field i1 should be known in foo.  In the Berkeley
Compiler, it is known if and only if there is a different struct with a
field i1 declared before main is declared !?  If there is such a field,
the type and offset of i1 are correct, otherwise you get an undefined field
reference error.

I discovered this while implementing a C compiler for a class and wanted to 
follow real C instead of the simplification of structures that our professor
recommended.  My solution was to say that the declaration of the structure was
exported to the block level of the first use of the forward declaration and
no further.   Any better ideas?

Bill Smith
princeton!siemens!wws



More information about the Comp.lang.c mailing list