cstructdeclarationtypedefundeclared-identifier

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


Solution

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