Panagiss
Panagiss

Reputation: 3740

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

Upvotes: 2

Views: 64

Answers (2)

klutt
klutt

Reputation: 31439

I would use this:

struct Node
{
  struct Node *next;
  struct Node *prev;
};

But then I happen to be one of those who does not like typedeffing structs without a good reason. Read more here: https://stackoverflow.com/a/54752982/6699433

Upvotes: 0

Luc A
Luc A

Reputation: 98

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;

Upvotes: 2

Related Questions