Self referring structure declaration

前端 未结 7 735
没有蜡笔的小新
没有蜡笔的小新 2021-01-21 04:00

The follwing declaration is valid.

struct node
{
    int a;
    struct node *next;
};

However, when we define the following, it gives error.

7条回答
  •  执念已碎
    2021-01-21 04:32

    You can't have structure that contains itself as a member:

    struct node
    {
        int a;
        struct node next;
    };
    

    Think about this question: if it is possible, what is the size of such structure? struct node contains struct node next as a member, then the member next would contain a member of type struct node as well, and so on and on and on... The size would be infinite.

提交回复
热议问题