2013-05-30 45 views
0

我知道其他人发布了相同的错误,但是我找不到类似于我的任何内容。我尝试过实施一些解决方案,但无法弄清楚它为什么不起作用。为什么警告:从不兼容的指针类型进行赋值?

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 
} 

为什么错误?

我在struct list_elem中犯了一个错字,prev和next应该是list_elems,而不是列表!!!!傻我。

+0

Indeeed,我typo'd! ..谢谢你肯定没有看到:) – cxzp

回答

2

你在struct liststruct list_elem之间混淆了。

它看起来像你只需要改变:

struct list_elem { 
    int value; 
    struct list *prev; 
    struct list *next; 
}; 

到:

struct list_elem { 
    int value; 
    struct list_elem *prev; 
    struct list_elem *next; 
}; 
+2

谢谢....我会在10分钟内接受你的答案。 – cxzp

1

list->footerstruct list_elem *list->header->nextstruct list *,所以这些任务将无法正常工作:

list->header->next = list->footer; //ERROR on this line 
list->footer->prev = list->header; //same ERROR on this line 

他们是不同类型的,所以他们的确是不兼容的。它看起来像你打算nextprevstruct list_elem *

3

根据您的声明,您将list->footer,这是list_elem*的内容分配到list->header->next,其类型为list*。这只是工作中的类型安全,类型不以任何方式兼容。

你可能意在声明会员prevlist_elemnextlist_elem*型,而不是list*

相关问题