Andreea
Andreea

Reputation: 1

How to typedef a forward declaration?

I need help with declaring some structures to use in my code. The idea is that I need to declare some structures that are included in each other and use typedef to have a good coding style.

I have tried to declare these:

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;

It doesn't work because the type "T_Tree" is not declared before, but I would like to find a way to declare them while keeping the type definitions shown above.

Upvotes: 0

Views: 62

Answers (2)

0___________
0___________

Reputation: 67476

Never (except function pointers) hide pointers in the typedef-s. It makes code more error prone and hard to read (you don't know when you see the declaration if something is a pointer or not).

struct node;

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

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

Upvotes: 3

Eric Postpischil
Eric Postpischil

Reputation: 222486

Insert typedef struct node *T_Tree; before the first declaration. Then remove T_tree from the last declaration.

That declares T_Tree to be a pointer to a struct node. You may declare a pointer to a struct even though the struct does not have a complete definition.

Upvotes: 2

Related Questions