C 为什么警告:来自不兼容指针类型的赋值?
我知道其他人也发布了相同的错误,但我找不到与我类似的错误。我已经尝试实施了一些解决方案,但无法找出它不起作用的原因C 为什么警告:来自不兼容指针类型的赋值?,c,arrays,pointers,error-handling,C,Arrays,Pointers,Error Handling,我知道其他人也发布了相同的错误,但我找不到与我类似的错误。我已经尝试实施了一些解决方案,但无法找出它不起作用的原因 struct list_elem { int value; struct list *prev; struct list *next; }; struct list{ struct list_elem *header; struct list_elem *footer; }; struct list_elem *list_elem
struct list_elem {
int value;
struct list *prev;
struct list *next;
};
struct list{
struct list_elem *header;
struct list_elem *footer;
};
struct list_elem *list_elem_malloc(void) {
struct list_elem *elem;
elem = malloc( sizeof(struct list_elem) );
return elem;
}
void list_init(struct list *list) {
list->header = list_elem_malloc();
list->footer = list_elem_malloc();
list->header->prev = NULL;
list->footer->next = NULL;
list->header->next = list->footer; //ERROR on this line
list->footer->prev = list->header; //same ERROR on this line
}
为什么会出错
我在结构列表元素中输入了一个错误,上一个和下一个应该是列表元素,而不是列表!!!!我真傻
list->footer
是一个结构列表元素*
,而list->header->next
是一个结构列表*
,因此这些赋值不起作用:
list->header->next = list->footer; //ERROR on this line
list->footer->prev = list->header; //same ERROR on this line
它们是不同的类型,因此它们确实不兼容。看起来您希望
next
和prev
成为struct list\u elem*
list->footer
是struct list\u elem*
,而list->header->next
是struct list*
,因此这些赋值将不起作用:
list->header->next = list->footer; //ERROR on this line
list->footer->prev = list->header; //same ERROR on this line
它们是不同的类型,因此它们确实不兼容。看起来您希望
next
和prev
成为struct list\u elem*
您正在将list->footer
的内容分配给list*
类型的list->header->next
,根据您的声明,这是一个列表元素*
。这只是工作中的类型安全,这些类型在任何方面都不兼容
您可能打算将
列表元素
的成员prev
和next
声明为类型列表元素*
,而不是列表*
您正在将列表->页脚
的内容分配给列表*
,根据您的声明,这是一个列表*
,类型为list*
。这只是工作中的类型安全,这些类型在任何方面都不兼容
您可能打算将
list\u elem
的prev
和next
成员声明为类型list\u elem*
,而不是list*
您将在struct list
和struct list\u elem
之间混淆
看起来您只需要更改:
struct list_elem {
int value;
struct list *prev;
struct list *next;
};
致:
你在
struct list
和struct list\u elem
之间搞混了
看起来您只需要更改:
struct list_elem {
int value;
struct list *prev;
struct list *next;
};
致:
不需要,我打错了。。谢天谢地,我绝对没有看到:)在需要的时候,我输入错了。。谢谢,我绝对没有看到:)谢谢。。。。如果可以的话,我会在10分钟内接受你的回答。谢谢。。。。如果可以的话,我会在10分钟内接受你的回答。