In what area is the identifier of the structure element?

C spec says

There are four types of areas: function, file, block, and function prototype.

Now, if I do the following outside of any function

struct A { int x; }; 

I understand that the identifier x displayed in the file area. And we use the namespace syntax to access the member, as the spec says

each structure or union has a separate namespace for its members (ambiguous in the type of expression used to access the element through an operator. or โ†’)

Let me make this clearer by adding a function

 struct A { int x; }; void f(void) { int i; } 

Then the identifiers, regions, and namespaces involved in this representation of the program (N / A "not applicable"):

 file scope ========================================== Ordinary | Members A | Tags | Labels ------------------------------------------ f | x | A | N/A | | | function scope of f ========================================= Ordinary | Members ? | Tags | Labels ----------------------------------------- N/A | N/A | N/A | | | | block scope #1 ========================================= Ordinary | Members ? | Tags | Labels ----------------------------------------- i | | | N/A | | | 

And the hierarchy of the region is โ€œblock region No. 1โ€ โ†’ โ€œscope of the function fโ€ โ†’ โ€œscopeโ€.

I once spoke to a C compiler writer, and he said that x not in any area. Can someone explain how this will work? How can we refer to x at all? Further quote (underline mine):

An identifier may indicate an object; function; tag or member of a structure, union, or enumeration; typedef name; label name macro name or macro parameter.

The same identifier can denote different objects at different points in the program.

For every other object that is indicated by an identifier, the identifier is visible (i.e., can be used) only in the area of โ€‹โ€‹the program text, called its area.

If we say that a member of the structure does not have a scope, then it is not visible and therefore cannot be used. But, obviously, we can use the elements of the structure. Did I miss something?

+5
source share
4 answers

As the standard says, this is in a separate namespace created for the type struct . This rule was added in C89, I think. Once upon a time, all member names shared a single namespace.

Perhaps the compiler writer is pedantic; namespace does not match scope . The definition of struct does not introduce scope because it does not contain variables; it contains members.

+5
source

I think the scope it refers to in this case is A. You are referring to the SomeA.x element, where SomeA is of type A.

0
source

x is in the region created by struct A , but A is in the global region. This is what โ€œspecโ€ causes the file area, but the name is misleading, because all the files that you include share this area.

0
source

The scope and namespace are two completely different concepts and should not be confused.

An instance of a structure or association has scope, but the names of the individual members do not; the concept of a sphere simply does not apply to them.

Arg, I was thinking about scales, not about scales.

0
source

All Articles