[comp.lang.c] Question on scope of struct/union tags

gnu@hoptoad.uucp (John Gilmore) (12/23/87)

I am testing compilers and need clarification on the exact scope of
declarations.  The 1 Oct 86 dpANS says, in section 3.1.2.1, that a
declaration's scope extends from the end of its *declarator* to the end
of the block or source file.  The question is, what if the identifier
is not declared by a *declarator*, but by a *struct-or-union-specifier*.
An example is the tag "s" in the second definition below:

	struct s {int x[5];};
	foo()
	{
		struct s {struct s *next; int i;};
		static struct s sa[2];
	 
		bar(sa[0].next->x[4]);
		bar(sa[0].next->i);
	}

I have two "ANSI" compilers (GCC and High-C); each accepts one of the
above calls to bar() and calls the other an error.  The question is
which one is "right".

I would prefer if the tag "s" was redefined immediately after its
appearance, so it can be used inside the {...} to make a self-
referential structure.  This seems like what most people would expect.
It also gives the same result as when there is no global "struct s".
The 1 Oct 86 standard does not seem to require this result, though.

As required by the standard, both compilers use the inner definition of
"s" if the line "struct s;" is inserted just after the { that opens
foo().

-- 
{pyramid,ptsfa,amdahl,sun,ihnp4}!hoptoad!gnu			  gnu@toad.com
  I forsee a day when there are two kinds of C compilers: standard ones and 
  useful ones ... just like Pascal and Fortran.  Are we making progress yet?
	-- ASC:GUTHERY%slb-test.csnet

gwyn@brl-smoke.ARPA (Doug Gwyn ) (01/06/88)

In article <3723@hoptoad.uucp> gnu@hoptoad.uucp (John Gilmore) writes:
-I would prefer if the tag "s" was redefined immediately after its
-appearance, so it can be used inside the {...} to make a self-
-referential structure.  This seems like what most people would expect.
-It also gives the same result as when there is no global "struct s".
-The 1 Oct 86 standard does not seem to require this result, though.

I think this has been fixed in the current draft.  Lots of things
pertaining to "incomplete types" and other aspects of typing were
cleaned up.