Alex D
Alex D

Reputation: 30465

Why can typedef'd names be used as the names of struct members?

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.

Upvotes: 8

Views: 172

Answers (2)

P.P
P.P

Reputation: 121407

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.

Upvotes: 9

MrBens
MrBens

Reputation: 1260

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)

Upvotes: 2

Related Questions