我收到很多“来自不兼容指针类型的赋值”警告,但我不知道为什么。 警告出现在这部分:
void addNode(linkedList* list, int board)
{
Node* newNode = createNode(board);
(list->last)->next = newNode; // this line has warning
newNode->prev = list->last; // this line has warning
}
结构体和其余代码是:
typedef struct{
int board;
struct Node* next;
struct Node* prev;
}Node;
typedef struct{
int length;
Node* first;
Node* last;
}linkedList;
Node* createNode(int board)
{
Node* node = (Node*)malloc(sizeof(Node));
node->next = NULL;
node->prev = NULL;
node->board = board;
return node;
}
linkedList* createList()
{
linkedList* list = (linkedList*)malloc(sizeof(linkedList));
list->first = NULL;
list->last = NULL;
list->length = 0;
return list;
}
最佳答案
这个...
typedef struct{ int board; struct Node* next; struct Node* prev; }Node;
...声明一个无标记结构类型,并将Node
定义为该类型的别名。它没有定义其成员next
和prev
指向的struct Node
类型。这并不妨碍声明指向此类类型的指针,但该类型与 Node
不同,并且与其不兼容。
假设其他地方没有任何 struct Node
的定义,最简单的解决方案就是添加该标记:
typedef struct Node {
int board;
struct Node* next;
struct Node* prev;
}Node;
这就是你的意思。
还要注意,您根本不需要 typedef
。添加标签后,您可以在任何地方将该类型引用为struct Node
。 typedef 的别名只是为了方便,我认为它被过度使用了。很多时候,typedef
带来的困惑多于其帮助。
关于c - 警告 : assignment from incompatible pointer type in linked list struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57312010/