ghiboz
ghiboz

Reputation: 8003

declare element in array that is the struct type

I have this struct:

typedef struct {
    int id;
    node_t * otherNodes;
} node_t;

where I need an array of nodes in my node....

but in the header file is not recognized: it tell me `unknown type name 'node_t'

how can I solve this?

thanks

Upvotes: 1

Views: 53

Answers (2)

user3716849
user3716849

Reputation: 11

I referenced defenition of struct list_head from kernel codes: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/include/linux/types.h?h=v5.10.84#n178

So I would write like this:

struct node {
    int id;
    struct node * otherNodes;
};

typedef struct node node_t;

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

In this typedef declaration

typedef struct {
    int id;
    node_t * otherNodes;
} node_t;

the name node_t within the structure definition is an undeclared name. So the compiler will issue an error.

You need to write for example

typedef struct node_t {
    int id;
    struct node_t * otherNodes;
} node_t;

Or you could write

typedef struct node_t node_t;

struct node_t {
    int id;
    node_t * otherNodes;
};

Or even like

struct node_t typedef node_t;

struct node_t {
    int id;
    node_t * otherNodes;
};

Upvotes: 2

Related Questions