Name Spaces
At any point in the source code, there will be a number of different identifiers that are "in scope", i.e. visible. The same identifier name can be used for different purposes within the same scope providing the identifiers are in different name spaces.
Four name spaces are defined in the C language:
- Ordinary identifiers, i.e objects, functions, typedef names and enum constants
- Tags of structures, unions and enumerations
- Members of each structure
- Label names
This means for example that:
- An identifier used for a variable can also be used, within the same scope, as a structure tag or a label.
- A structure tag cannot also be used as a union or enum tag.
- The members of a structure or union only have to be unique within that structure; the same member name can be used in more than one type of structure.
- The name of a structure tag can also be used as a typedef
If these rules are violated, QA C and indeed any compiler, will generate an error warning. It is permissible within the rules of C to use the same identifier for more than one purpose providing the name spaces are respected; but it is usually unwise to do so and programming guidelines often specifically prohibit this sort of thing, e.g.:
- Using the same identifier in different name spaces; for example declaring the same identifier as both a structure tag and as a typedef.
- Redefining an identifier by hiding an identifier declared at outer scope, with a different declaration at inner scope.
- Using the same identifier to mean different things in different scopes.
Note that macro identifiers are not part of any name space, because they are a preprocessor feature only and are not subject to the same rules of scope.
Index