Nested Structure Syntax

I have a quick question ... is there a difference in them:

struct myinnerstruct
{
    int x;

};

struct mystruct
{
    struct myinnerstruct m;
    int y;
};

AND THIS

struct mystruct
{
    int x;
    struct myinnerstruct
    {
        int y;
    };
    struct myinnerstruct m; 
};

They both work, as far as I can tell, but I wonder if there is a reason to choose one or the other. Thanks

+5
source share
2 answers

The difference is that the second is invalid.

The material between {and }in the structure declaration is a sequence of member declarations. Your

    struct myinnerstruct
    {
        int y;
    };

- type declaration; he does not declare a member of the closing structure, so in this context he is illegal.

What you can do is:

struct mystruct
{
    int x;
    struct myinnerstruct
    {
        int y;
    } m;
};

m , ; struct myinnerstruct. , , . struct myinnerstruct struct mystruct; . .

, struct myinnerstruct - , :

struct mystruct
{
    int x;
    struct
    {
        int y;
    } m;
};

y struct mystruct.

, struct innerstruct , , .

, struct innerstruct .

C99 standard ( PDF), 6.2.1, 2, :

, , (.. ) . , . : , , . ( - , .)

C90 C11 , , .

{ } - , , , ; - . , struct myinnerstruct , . , , , ; . , .

+7

.

, , , .

, , . , , , .

+1

All Articles