Search code examples
cdata-structuresstructlinked-listtypedef

I cannot typedef a pointer to structure that has already been typedef-ed


I am a bit comfused about the row of these declarations. I want to make a linked-list for my program but for some reason it keeps putting error when I try to compile it. Basically this will be my main node model and I also have another struct after it (WITHOUT TYPEDEF) for my list. Which compiles just fine. I don't know what's wrong.

I have already tried to put the typedef over the struct student.

typedef struct
{
    char name[50];
    int id;
    node next;

}student;

typedef student* node;

typedef struct listR* list;

struct listR
{
    node head,tail;
    int size;

};

error: unknown type name 'node' warning: initialization make pointer from integer without a cast


Solution

  • The compiler doesn't know what a node is, because you create the node type after creating the structure.

    You can do either :

    typedef struct node node;
    
    struct node
    {
      char name[50];
      int id;
      node* next;
    };
    

    To tell the compiler what a node is,

    Or

    typedef struct node {
        char name[50];
        int id;
        struct node* next;
    } node;