Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested structures

Tags:

c

struct

The following code compiles on a C++ compiler.

#include <cstdio>
int main()
{
    struct xx
    {
        int x;
        struct yy
        {
            char s;
            struct xx *p;
        };
        struct yy *q;
    };
}

Would there be any difference in behavior while compiling with a C compiler?
i.e. would there be any compiler error?

like image 537
TL36 Avatar asked Nov 29 '09 14:11

TL36


People also ask

What are nested structures in go?

In Go, A structure which is a field of another structure is known as the Nested Structure.

What is nested structure in Java?

In Java, a nested class is any class whose definition is inside the definition of another class. A nested class is a member of its enclosing class and, as such, has access to other members of the enclosing class, even if they are declared private. Nested classes can be either named or anonymous.

What is the purpose of nested structure in C++?

C provides us the feature of nesting one structure within another structure by using which, complex data types are created. For example, we may need to store the address of an entity employee in a structure.

What is the syntax of nested structure?

Here is the syntax to create nested structures. structure tagname_1 { member1; member2; member3; ... membern; structure tagname_2 { member_1; member_2; member_3; ... member_n; }, var1 } var2; Note: Nesting of structures can be extended to any level.


2 Answers

The code in your post is obviously incomplete, just declarations, so it is hard to say anything conclusive.

On obvious difference is that in C++ the inner struct type will be a member of outer struct type, while in C language both struct types will be members of the same (enclosing) scope. (BTW, was it your intent to declare them locally in main?).

In other words, in C++ the code that follows would have to refer to the structs as xx and xx::yy, while in C they would be just xx and yy. This means that the further code would look different for C and C++ and if it will compile in C++, it wouldn't compile in C and vice versa.

Added: C language prohibits declaring struct types inside other structs without declaring a member of that type. So, you struct yy declaration is illegal in C and will produce compiler diagnostic message. If you wanted your code to become legal in both C and C++, you'd have to combine the struct yy declaration with some data member declaration. In your case that could be pointer q:

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

The above is legal in both C and C++ (taking into account the differences I explained earlier), but your original declaration is not legal in C.

like image 135
AnT Avatar answered Nov 28 '22 10:11

AnT


C does not permit you to nest a type declaration inside a function definition. Also, to eliminate the warning about "does not declare anything, you should merge the declarations of type struct yy and member q. The following compiles with gcc with max warnings on:

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

int main()
{
  return 0;
}
like image 25
Norman Ramsey Avatar answered Nov 28 '22 11:11

Norman Ramsey