Hi
I want to know the relationship between "name space" and "lexical scope" (or scope) in the C language. In the C standard
Code:
If more than one declaration of a particular identifier is visible
at any point in a translation unit, the syntactic context
disambiguates uses that refer to different entities. Thus, there are
separate name spaces for various categories of identifiers, as
follows:
* label names (disambiguated by the syntax of the label declaration
and use);
* the tags of structures, unions, and enumerations (disambiguated by
following any/11/ of the keywords struct , union , or enum );
* the members of structures or unions; each structure or union has a
separate name space for its members (disambiguated by the type of the
expression used to access the member via the . or -> operator);
* all other identifiers, called ordinary identifiers (declared in
ordinary declarators or as enumeration constants).
I'm confused about their relationships. How does C establish different name spaces?
For example, in a translation unit, if all the tags of structures, unions, and enumerations are in the same name space, then what about the following code:
Code:
int
main(void)
{
struct test{
int i;
char c;
};
return 0;
}
void
myfunc(void)
{
struct test{
int i;
char c;
};
}
the structure tag "test" is in the same name space, but in different lexical scopes. Does this mean the same idendifier can appear more than once in a name space if each has different lexical scope? Then how about this:
Code:
int i;
int i;
int
main(void)
{
return 0;
}
the variable name "i" is in the same name space, and both have file scope, which are overlapped.
So is "lexical scope" a prerequisite when discussing "name space", or is "name space" a prerequisite when discussing "lexical scope"?