在什么范围内是一个结构成员标识符放? [英] In what scope is a struct member identifier put?

查看:147
本文介绍了在什么范围内是一个结构成员标识符放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的C规范说

有四种作用域:函数,文件,数据块和函数原型。

There are four kinds of scopes: function, file, block, and function prototype.

现在,如果我做的任何功能之外以下

Now if I do the following outside any function

struct A {
  int x;
};

我的理解是,该标识符 X 在文件范围可见。而且,我们使用一个命名空间语法来访问该成员,作为规范说

My understanding is that the identifier x is visible at file-scope. And that we use a namespace syntax to access the member, as the spec says

每个结构或联合拥有其成员的单独的名称空间(通过使用通过或进入该成员的前pression类型消除歧义 - >运算符)

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)

让我们使这更清晰的通过增加一个功能

Let's make this more clearer by adding a function

struct A {
  int x;
};

void f(void) {
  int i;
}

然后标识,作用域和命名空间参与该计划的重presentation是(N / A是不适用):

Then the identifiers, scopes and namespaces participating in this program's representation are (N/A is "not applicable"):

file scope
==========================================
Ordinary  |  Members A  |  Tags  |  Labels
------------------------------------------
 f        | x           | A      | N/A
          |             |        |


function scope of f
=========================================
Ordinary  |  Members ? |  Tags  |  Labels
-----------------------------------------
 N/A      | N/A        | N/A    |
          |            |        |


block scope #1
=========================================
Ordinary  |  Members ? |  Tags  |  Labels
-----------------------------------------
 i        |            |        | N/A
          |            |        |

和范围层次是块范围#1 - >的F功能范围 - >文件范围

And the scope hierarchy is "block scope #1" -> "function scope of f" -> "file scope".

我曾经跟一个C编译器的作家,他说 X 不进任何范围。任何人都可以请解释一下这是如何工作的?我们怎么能指 X 以任何方式在所有的呢?而且报价(强调雷):

I once talked with a C compiler writer, and he said that x is not into any scope. Can anyone please explain how this would work? How could we refer to x in any way at all then? Further quotation (emphasize mine):

这是鉴定网络连接器可以表示的对象;一个功能;标签或结构的成员的,联合或枚举; typedef名;标签名称;宏名;或宏参数。

An identifier can denote an object; a function; a tag or a member of a structure, union, or enumeration; a typedef name; a label name; a macro name; or a macro parameter.

相同的标识符可以在程序的不同点表示不同的实体。

The same identifier can denote different entities at different points in the program.

有关该标识符指定各不同的实体,所述标识符是可见的(即,可以使用)仅在程序文本的区域称为其范围。

For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope.

如果我们说一个结构的成员不具有范围,那么它是不可见的,为此,不能使用。但很明显,我们的可以的使用结构成员。我缺少什么?

If we say that a member of a struct has no scope, then it is not visible and therefor cannot be used. But obviously, we can use struct members. Am I missing anything?

推荐答案

作为标准说,这是在为结构键入创建一个独立的名字空间。在C89加入这条规则,我想。 <一href=\"http://stackoverflow.com/questions/3881064/anonymous-structures-in-c-found-in-unix-kernel/3881213#3881213\">Once曾几何时,所有成员的名字都共享一个名字空间。

As the standard says, it's in a separate name space created for the struct type. This rule was added in C89, I think. Once upon a time, all member names shared a single name space.

也许编译器作者当时正在迂腐;一个命名空间是不一样的的范围即可。 A 结构定义并不引入作用域,因为它不成立的变量;它拥有成员。

Maybe the compiler writer was being pedantic; a name space is not the same as a scope. A struct definition does not introduce a scope as it doesn't hold variables; it holds members.

这篇关于在什么范围内是一个结构成员标识符放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆