Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 964
Alex D Avatar asked Apr 29 '20 14:04

Alex D


People also ask

Can typedef and struct have same name?

You can't "typedef a struct", that doesn't mean anything.

What is difference between typedef and struct?

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.

What does typedef struct mean?

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.

Should you typedef structs?

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.


2 Answers

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.

like image 80
P.P Avatar answered Dec 04 '22 00:12

P.P


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)

like image 28
MrBens Avatar answered Dec 04 '22 01:12

MrBens