Home > Software engineering >  declare element in array that is the struct type
declare element in array that is the struct type

Time:12-13

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

CodePudding user response:

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;
};

CodePudding user response:

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;
  • Related