【问题标题】:Why warning: assignment from incompatible pointer type? [closed]为什么警告:来自不兼容的指针类型的赋值? [关闭]
【发布时间】:2013-05-26 02:37:21
【问题描述】:

我知道其他人也发布了相同的错误,但我找不到与我类似的任何内容。我已经尝试实施一些解决方案,但无法弄清楚为什么它不起作用。

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,而不是列表!!!!傻我。

【问题讨论】:

  • 确实,我错了! ..谢谢绝对没有看到:)

标签: c arrays pointers error-handling


【解决方案1】:

您将list->footer(根据您的声明为list_elem*)的内容分配给list->header->next,其类型为list*。这只是工作中的类型安全,类型不兼容。

您可能打算将list_elem 的成员prevnext 声明为list_elem* 类型而不是list*

【讨论】:

    【解决方案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;
    };
    

    【讨论】:

    • 谢谢....我会在 10 分钟内接受您的答复。
    【解决方案3】:

    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
    

    它们是不同的类型,因此它们确实不兼容。看起来您打算将 nextprev 设为 struct list_elem *

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-16
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2013-06-21
      • 1970-01-01
      • 2011-06-21
      相关资源
      最近更新 更多