[net.lang.c] Who says infinite structures are impossible?

wws@siemens.UUCP (William W Smith) (07/23/84)

In the Berkeley Unix C compiler, the following declaration creates an
infinitely recursive structure:


struct x {
	int a;
	struct x { int b,c;} f;
        } s;


The compiler is more than happy to generate code for:

s.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.f.a = 727;

and, sizeof(struct x) returns 12 on a VAX.

Can anybody find a good use for this "feature?"

Bill Smith
princeton!siemens!wws

west@sdcsla.UUCP (07/27/84)

Hey, that one was fun with dbx ("print s")!!

But seriously, it is clearly a compiler fault, since the "C Reference Manual"
clearly states (section 8.5, page 197):
	... names of tags and members must be mutually distinct.

The example structure (given here
	struct x {
		int a;
		struct x { int b,c;} f;
		} s;
for clarity) contains a structure with the same structure tag name as is
given the whole structure.

No scoping of these names within structures is implied, nor should any
be inferred.

It is unfortunate that both the C compiler (cc on 4.2bsd) and lint
let this go by, and in fact give misleading warnings about "s.f.b"
and "s.f.c" having illegal members.

In short, I'd never belong to any structure that had itself as a member.

	-- Larry West, UC San Diego, Institute for Cognitive Science
	-- decvax!ittvax!dcdwest!sdcsvax!sdcsla!west
	-- ucbvax!sdcsvax!sdcsla!west
	-- west@NPRDC		{{ NOT: <sdcsla!west@NPRDC> [aarg!] }}