Nested structures

前端 未结 6 2012
旧时难觅i
旧时难觅i 2020-12-03 08:42

The following code compiles on a C++ compiler.

#include 
int main()
{
    struct xx
    {
        int x;
        struct yy
        {
                    


        
6条回答
  •  囚心锁ツ
    2020-12-03 08:58

    Based on my brief research and the error message posted by Otto, it appears that C doesn't allow structures to be general-purpose namespace containers like C++ classes and structures are (naturally, because C doesn't even support classes). So you can't nest structure definitions in C. You would have to declare the inner structure outside of the outer structure declaration like this:

    struct yy
    {
            char s;
            struct xx *p;
    };
    struct xx
    {
        int x;
        struct yy *q;
    };
    

    I see that the structures cross-reference each other. So if this is even possible in C, you may have to pre-declare the later structure with a line like:

    struct xx;
    

    (above both other declarations).

提交回复
热议问题