另一种结构中的结构范围内声明/定义 [英] Scope of structure declared/defined within another structure

查看:149
本文介绍了另一种结构中的结构范围内声明/定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的结构阅读,我意识到另一个结构中声明结构范围相同,包含结构。我以为它只是包含结构范围内。我从这个链接此IM pression。它说,有4个名称空间,其中之一是一个结构的成员。我逻辑推断内部结构仅在外层范围之内。

Reading from Nested structures, I realized that structure declared within another structure is scoped the same as the containing structure. I thought it is only scoped within the containing structure. I got this impression from this link. It says there are 4 namespaces and one of them is members of a structure. I "logically" inferred that inner structures are only scoped within the outer one.

任何人都可以提供作为范围规则在这种情况下,究竟是如何运作的标准的参考?并允许内部结构是包含结构外可见的任何理由?如果它是可见的,为什么不申报外结构...

Can anyone provide a reference to the standard as to exactly how the scope rule works in this case? And any rationale for allowing inner struct to be visible outside the containing structure? If it is visible, why not just declare the struct outside...

问题2 :对于术语,当我提供了一个结构的成员,说

Question 2: For terminology, when I provide the members of a structure, say

struct out{
    int a, b;
    char c, d;
    struct in{
        int a, b;
    }e;
};

是我提供的定义的两个结构体退出结构;还是我提供的声明的两个?我理解功能和原始数据类型,但真不明白这里的结构秒。

Am I providing a definition for both struct out and struct in; OR am I providing a declaration for both? I understand the difference for functions and primitive data types but not really clear here for struct s.

修改:有用的链接,我只是发现SO:嵌套结构中的C和C ++

EDIT: Useful link I just found on SO: Nested structures in C and C++.

但是,它不提供任何理由。现在我怀疑,如果有一个用于C ...

But there it does not provide any rationale. And now I doubt if there is one for C...

推荐答案

有关的的 为范围,规则C99标准草案6.2.1 的标识的作用域的,并在段落 7 的说(的重点煤矿的):

For the C99 draft standard the rules for scope are covered in 6.2.1 Scopes of identifiers and in paragraph 7 says(emphasis mine):

结构,联合和枚举标签具有范围,只是标签的类型说明符的外观声明标记之后开始。每个枚举常量的范围,只是其定义枚举的枚举列表出现之后开始。任何其他标​​识符有范围,只是在其声明完成后开始。

Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator.

该范围结束以下段所设定的规则的 4 的:

The scope ends following the rules laid out in paragraph 4:

每隔标识符范围由其声明的位置决定(在
  声明符或类型说明符)。如果声明标识符声明符或类型说明符出现任何块或参数列表之外,标识符具有文件范围,这在翻译单元的结束终止。如果声明标识符声明符或类型说明符出现一个块中或在函数定义的参数声明的列表中,标识符有块作用域,它终止在相关块的结尾。 [...]

Every other identifier has scope determined by the placement of its declaration (in a declarator or type specifier). If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit. If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the end of the associated block. [...]

至于理,它可能有做的事实,不像C ++,在C我们没有范围解析运算符(

As for the rationale, it probably has to do with the fact that unlike C++, in C we don't have the scope resolution operator(::).

至于问题 2 您提供的定义,并在你的榜样的声明,这将只是一个声明的例子:

As for question 2 you are providing a definition and a declaration in your example, this would be an example of just a declaration:

struct out ;

另请参见:<一href=\"http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration\">What是一个定义和声明有何区别?。

对于所有的C和C之间的不兼容性++,你可以检查出的清单的之间的不兼容性
ISO C和ISO C ++
它涵盖了这个主题这里

For a list of all the incompatibilities between C and C++ you can check out Incompatibilities Between ISO C and ISO C++ it covers this subject here.

这篇关于另一种结构中的结构范围内声明/定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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