首页 > 解决方案 > 在 C 中创建递归数据结构的方法

问题描述

标签: crecursionstructtypedefdefinition

解决方案


Person is not defined yet, since the typedef only takes effect after the semicolon concluding it. To refer to the struct from within itself, use struct Person. The following code compiles with no errors on GCC 10.2.0.

typedef struct Person {
    char* name;
    struct Person* mother;
    struct Person* father;
} Person;

int main() {
    Person Kathy = { "Kathy", NULL, NULL };
    Person Bill = { "Bill", NULL, NULL };
    Person Bob = { "Bob", &Kathy, &Bill };
    return 0;
}

推荐阅读