Consider the situation:

struct a { int id; struct b { int x, y, z; }; struct b obj; }; struct b B;// Так должно быть нельзя... 

What does the Standard say in this situation?

Is it possible to declare a variable of type struct b in the scope where the definition of struct a is located

Logically, with the structure described inside the description of another structure, everything should be exactly the same as with variables and functions — such an embedded declaration should have a local scope.

But MinGW does that. Maybe this indefinite behavior?

    1 answer 1

    Language C prohibits declaring one structure inside another without simultaneously declaring a field of this type. Your code is wrong.

    Here it is possible

     struct a { int id; struct b { int x, y, z; } obj; }; struct b B; // OK 

    At the same time, the “internal” structure type struct b is still declared in the covering scope. In C there is no such "local" scope as "structure".


    By the way:

    In addition to declaring new types inside structures, the C language (unlike C ++) also allows you to declare new types inside type conversion operators and inside the sizeof operator. Types declared in this way will be visible from the outside, in the covering scope.

     size_t n = sizeof(struct b { int x, y, z; }); struct bx; // OK 

    and

     void *p = (struct b { int x, y, z; } *) 0; struct bx; // OK 
    • Understood thanks. What about function declarations? Are functions declared inside other functions visible outside their ad scope? - user294535
    • Maxim: I mean? How are you going to screw the function declaration here? - AnT
    • Here - no. But it is usually convenient to declare auxiliary functions inside the function in order to use them, but at the same time not to clog the visibility area above. - user294535
    • @Maxim: In standard C, there is no such thing. - AnT
    • Standard C is C89? I am interested in modern dialects - C99 and C11 - user294535