I am trying to create a Linkedlist using C, and I keep getting this error when I try to compile it,

warning: assignment to ‘struct ListNode *’ from incompatible pointer type ‘ListNode *’ [-Wincompatible-pointer-types]

These are my structs:

typedef struct {
    void *data;
    struct ListNode *next;
    struct ListNode *prev;
} ListNode;

typedef struct {
    ListNode *head;
    ListNode *tail;
    int size;
} LinkedList;

the whole list of warnings is shown down below:

linkedList.c:26:21: warning: assignment to ‘struct ListNode *’ from incompatible pointer type ‘ListNode *’ [-Wincompatible-pointer-types]
   26 |         newNd->next = list->head;
      |                     ^
linkedList.c: In function ‘printList’:
linkedList.c:47:16: warning: assignment to ‘ListNode *’ from incompatible pointer type ‘struct ListNode *’ [-Wincompatible-pointer-types]
   47 |         currNd = currNd->next;
      |                ^
linkedList.c: In function ‘removeStart’:
linkedList.c:59:16: warning: assignment to ‘ListNode *’ from incompatible pointer type ‘struct ListNode *’ [-Wincompatible-pointer-types]
   59 |     list->head = temp->next;
      |                ^
linkedList.c: In function ‘insertLast’:
linkedList.c:88:18: warning: assignment to ‘ListNode *’ from incompatible pointer type ‘struct ListNode *’ [-Wincompatible-pointer-types]
   88 |             temp = temp->next;
      |                  ^
linkedList.c:90:20: warning: assignment to ‘struct ListNode *’ from incompatible pointer type ‘ListNode *’ [-Wincompatible-pointer-types]
   90 |         temp->next = newNd;
      |                    ^
linkedList.c: In function ‘removeLast’:
linkedList.c:109:14: warning: assignment to ‘ListNode *’ from incompatible pointer type ‘struct ListNode *’ [-Wincompatible-pointer-types]
  109 |         curr = curr->next;
      |              ^

If any more information is required, I am happy to add more.

3

There are 3 best solutions below

0
Daniel Walker On

You created a typedef of an anonymous struct when you defined ListNode. It is therefore not the same as struct ListNode. What you need to do is

typedef struct ListNode {
    …
} ListNode;
0
Chris On

If you want to have a pointer to your struct as a member of that struct, it needs to have a name before the typedef.

typedef struct ListNode {
    void* data;
    struct ListNode* next;
    struct ListNode* prev;
} ListNode;

Alternately, start by typedefing:

typedef struct ListNode ListNode;

struct ListNode {
    void* data;
    ListNode* next;
    ListNode* prev;
};

Your struct typedefed to LinkedList doesn't require this because it doesn't contain a pointer to a LinkedList as a member.

4
Vlad from Moscow On

In this typedef declaration

typedef struct {
    void* data;
    struct ListNode* next;
    struct ListNode* prev;
} ListNode;

there are introduced two different types: the incomplete structure type struct ListNode and an unnamed (without structure tag name) structure with the typedef name ListNode. So the compiler issues errors when you are trying to assign a pointer of one type to a pointer of other type.

To update correctly code you could write for example

typedef struct ListNode{
    void* data;
    struct ListNode* next;
    struct ListNode* prev;
} ListNode;

or

typedef struct ListNode ListNode;
struct ListNode{
    void* data;
    ListNode* next;
    ListNode* prev;
};

or

struct ListNode{
    void* data;
    struct ListNode* next;
    struct ListNode* prev;
};

typedef struct ListNode ListNode;

Pay attention to that sometimes the term "anonymous structure" is applied incorrectly to unnamed structures (structures without tag names). According to the C Standard (6.7.2.1 Structure and union specifiers)

13 An unnamed member whose type specifier is a structure specifier with no tag is called an anonymous structure; an unnamed member whose type specifier is a union specifier with no tag is called an anonymous union. The members of an anonymous structure or union are considered to be members of the containing structure or union, keeping their structure or union layout. This applies recursively if the containing structure or union is also anonymous.