-1

I am woundering the following code:

#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;
}

Should the scope of strut test2, test3 and the enum being within the struct test1? But the compiler didn't report any error, by the way the compiler the MinGW GCC.

Tiago Sippert
  • 1,324
  • 7
  • 24
  • 33

1 Answers1

1

In C such code is allowed, since all types are declared in a single namespace.

In C++ compiler should produce an error, since struct test2 in declared in scope of struct test1. In C++ your variable should be declared as follows:

    test1::test2 hsd;
Vitaliy Y.
  • 11
  • 2