I have just discovered that both gcc
and clang
accept the following code:
typedef int blah;
struct s { char blah; };
However, they reject this, since a type name is being used as an identifier:
typedef int blah;
char blah;
Does this mean that the typedef'd name is not visible inside the struct definition? No, because this works in both gcc and clang:
typedef int blah;
struct s { blah blah; }
I am looking in the C99 standard and can't find anything which clarifies why a typedef'd name can be used as the name of a struct member, but not of a variable in the same scope.
Can someone explain why this is? A reference to any applicable standard would be appreciated.
You can't "typedef a struct", that doesn't mean anything.
Basically struct is used to define a structure. But when we want to use it we have to use the struct keyword in C. If we use the typedef keyword, then a new name, we can use the struct by that name, without writing the struct keyword.
The C language contains the typedef keyword to allow users to provide alternative names for the primitive (e.g., int) and user-defined (e.g struct) data types. Remember, this keyword adds a new name for some existing data type but does not create a new type.
PLEASE don't typedef structs in C, it needlessly pollutes the global namespace which is typically very polluted already in large C programs. Also, typedef'd structs without a tag name are a major cause of needless imposition of ordering relationships among header files.
Members of structures and ordinary variables are in different namespaces. That's why having two ordinary variables with same identifier name fails whereas if the same identifier name is used in a struct member and an ordinary variable is fine.
The C standard defines distinct namespaces:
6.2.3 Name spaces of identifiers
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 any32) 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).
(The last two bullet items directly address this question)
Yes, typedef
'ed identifiers share the name space with ordinary identifiers. 6.7.8 Type definitions:
[...] A typedef name shares the same name space as other identifiers declared in ordinary declarators.
Taken from here: https://www.spinellis.gr/cscout/doc/name.html
C has 4 different namespaces. These are not the namespaces from C++, which are accessed by using the keyword namespace
. Rather, these are seperate areas for symbols:
Tags for a struct/union/enum
Members of struct/union (actually a separate namespace is assigned to each struct/union)
Labels
Ordinary identifiers (termed objects in the C standard)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With