作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚发现 gcc
和 clang
接受以下代码:
typedef int blah;
struct s { char blah; };
typedef int blah;
char blah;
typedef int blah;
struct s { blah blah; }
最佳答案
结构体成员和普通变量在不同的命名空间中。这就是为什么有两个具有相同标识符名称的普通变量会失败,而如果在 struct 成员中使用相同的标识符名称并且普通变量很好。
C 标准定义了不同的命名空间:
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).
typedef
'ed 标识符与普通标识符共享 namespace 。
6.7.8 Type definitions :
[...] A typedef name shares the same name space as other identifiers declared in ordinary declarators.
关于c - 为什么可以使用 typedef 的名称作为结构成员的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61504537/
我是一名优秀的程序员,十分优秀!