【问题标题】:what is the scope of nested structure? [duplicate]嵌套结构的范围是什么? [复制]
【发布时间】:2013-06-28 14:49:54
【问题描述】:

我正在破坏以下代码:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  struct test1
  {
    struct test2
    {
      struct test3
      {
        enum TokenType
        {
          COMMA_TOKEN, EOF_TOKEN,
        } token_value;
      } b;
    } c;
  };

  struct test2 hsd;
  hsd.b.token_value = 2;

  return 0;
}

strut test2、test3和enum的范围应该在struct test1之内吗?但是编译器没有报错,顺便编译了MinGW GCC。

【问题讨论】:

    标签: c struct enums scope nested


    【解决方案1】:

    在 C 中这样的代码是允许的,因为所有类型都在一个命名空间中声明。

    在 C++ 编译器中应该产生错误,因为 struct test2 在 struct test1 的范围内声明。在 C++ 中,您的变量应声明如下:

        test1::test2 hsd;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2018-10-12
      • 2013-01-05
      • 2010-09-18
      • 1970-01-01
      • 2011-08-13
      相关资源
      最近更新 更多