如何typedef一个前向声明?

时间:2019-04-27 13:47:19

标签: c struct typedef

我需要帮助声明一些要在我的代码中使用的结构。我的想法是,我需要声明一些相互包含的结构,并使用typedef来具有良好的编码风格。

我试图声明这些:

typedef struct cell
{
    T_Tree list_of_sons;
    struct cell *next;
}ListCell, *T_List, **Adr_List;

typedef struct node
{
    int value;
    T_List list;
}Node, *T_Tree;

这是行不通的,因为之前没有声明类型“ T_Tree”,但我想找到一种在保留上面显示的类型定义的同时声明它们的方法。

2 个答案:

答案 0 :(得分:3)

从不(除函数指针外)在typedef-s中隐藏指针。它使代码更易于出错且难以阅读(您看不到何时看到声明是否是指针)。

struct node;

typedef struct cell
{
    struct node *list_of_sons;
    struct cell *next;
}ListCell;

typedef struct node
{
    int value;
    ListCell *list;
}Node;

答案 1 :(得分:2)

在第一个声明之前插入typedef struct node *T_Tree;。然后从最后一个声明中删除T_tree

这声明T_Tree是指向struct node的指针。即使struct没有完整的定义,您也可以声明指向struct的指针。

相关问题