What is the meaning of a structure inside another without a tag name?

I just came across a piece of code: -

struct a {
    int mem1;
    char mem2;

    struct {
        int inner_mem1;
        int inner_mem2;
    };
};

And I found that a code snippet using internal structure elements directly using the name of an external struct variable !!! eg:

struct a *avar;
....
avar->inner_mem1

Whether it is legal, the code compiles and works fine !. What is the purpose of using it in this way? Any specific scenarios?

Please let me know your thoughts.

+5
source share
1 answer

This is called an anonymous structure:

; . . , .

C, C99, , ( ). , .

+7

All Articles