[comp.std.c] scope of a structure tag

holub@violet.berkeley.edu (01/07/89)

I'm confused about the scope of a structure tag. Is the following legal
in ANSI C:

	struct tag1
	{
		struct tag2
		{
			int a, b;	
		} x;
	}

	struct tag3
	{
		struct tag2 y;
	}

or does the inner structure have to be redefined in tag3?

	- Allen Holub
	 holub@violet.berkeley.edu
	 ...!ucbvax!violet!holub

gwyn@smoke.BRL.MIL (Doug Gwyn ) (01/07/89)

In article <18799@agate.BERKELEY.EDU> holub@violet.berkeley.edu () writes:
-I'm confused about the scope of a structure tag. Is the following legal
-in ANSI C:
-	struct tag1
-	{
-		struct tag2
-		{
-			int a, b;	
-		} x;
-	}
-	struct tag3
-	{
-		struct tag2 y;
-	}
-or does the inner structure have to be redefined in tag3?

I don't understand your example, which is syntactically invalid.
Assuming you put semicolons where they belong and that the two
"outer" structures are declared within the same scope (the same
file, block, or function prototype, depending on the context),
then the first "struct tag2" is still in scope when the second
one is encountered, so it is a complete type at that point.

holub@violet.berkeley.edu (01/10/89)

To: agate!ucbvax!decwrl!purdue!haven!adm!smoke!gwyn
Subject: Re: scope of a structure tag
Newsgroups: comp.std.c
In-Reply-To: <9303@smoke.BRL.MIL>
References: <18799@agate.BERKELEY.EDU>
Organization: University of California, Berkeley
Cc: 
Bcc: 

In article <9303@smoke.BRL.MIL> you write:
>
>In article <18799@agate.BERKELEY.EDU> holub@violet.berkeley.edu () writes:
>>I'm confused about the scope of a structure tag. Is the following legal
>>in ANSI C:
>>	struct tag1 { struct tag2 { int a, b;	} x;  }
>>	struct tag3 { struct tag2 y;  }	
>>
>>or does the inner structure have to be redefined in tag3?
>
>I don't understand your example, which is syntactically invalid.
>Assuming you put semicolons where they belong and that the two
>"outer" structures are declared within the same scope (the same
>file, block, or function prototype, depending on the context),
>then the first "struct tag2" is still in scope when the second
>one is encountered, so it is a complete type at that point.

Other than the obvious fact that I forgot a semicolon after the close braces,
is there anything else invalid here? It looks okay to me. I think that you've
answered my question, but just to make sure, let me restate it. Does the scope
of a structure tag extend from the point of declaration to the end of the cur-
rent compound statement or whatever (as I suspect) or can you define substruc-
tures with identical tags but different fields? My confusion stems from the fact
that an identifier can be used in two separate structure declarations and be
considered unique because the identifer must be qualified by a reference to the
structure. This doesn't seem to apply to tags, however. That is, I am assuming
that the following is an illegal redefinition of tag2, even though the two
foo's are okay:

    fred()
    {
	struct tag1 { struct tag2 { int  a, b; } x;  int foo; }struct_one;
	struct tag3 { struct tag2 { long a, b; } x;  int foo; }struct_two;
    }

but by the same token, the following is okay:

    fred()
    {
	struct tag1 { struct tag2 { int  a, b; } x;  int foo; }struct_one;
	struct tag3 { struct tag2 y;  		     int foo; }struct_two;
    }

Is my assumption correct?

gwyn@smoke.BRL.MIL (Doug Gwyn ) (01/11/89)

In article <18915@agate.BERKELEY.EDU> holub@violet.berkeley.edu () writes:
>Does the scope of a structure tag extend from the point of declaration to
>the end of the current compound statement or whatever (as I suspect) or
>can you define substructures with identical tags but different fields? 

Structure tags have file, block, or (with ANSI C) prototype scope,
depending on where they occur.  The {} in a structure declaration
do not delimit a block.

>Is my assumption correct?

Yes.