如何输入定义前向声明?

问题描述 投票:0回答:2

我需要帮助来声明我的代码中使用的一些结构。我的想法是,我需要声明一些彼此包含的结构,并使用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”,但我想找到一种方法来声明它们,同时保持上面显示的类型定义。

c struct typedef
2个回答
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;

2
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.