Tag names (for structs, unions, and enums) occupy a different name space from identifiers (function, object, and typedef names). Tag names are disambiguated by the presence of the struct
, union
, or enum
keywords1.
Note that struct and union member names occupy yet another namespace and are disambiguated in expressions by the use of the .
or ->
component selection operators, so the following code is legal (if a really bad idea):
struct foo { int foo; };
struct foo foo;
foo.foo = some_value();
The type name struct foo
is disambiguated by the presence of the struct
keyword, and the member name foo.foo
is disambiguated by the presence of the .
operator.
Here are the things you cannot do:
- You cannot use the same tag name for a struct and a union, or a struct and an enum, etc; IOW, you cannot do:
struct foo { ... };
enum foo { ... };
union foo { ... };
in the same code;
- You cannot use the same name for a typedef name and an object name:
typedef struct foo { int foo; double bar; } foo;
foo foo;
- You cannot use the same name for an enumeration constant and an object name:
enum blurga { foo, bar, bletch } foo;
- You cannot use the same name for a function name and an object name:
int foo() { ... }
int bar()
{
int foo = foo();
}
1. I'm assuming this is also the case for the class
keyword in C++, but I don't have that reference handy.