从c中的不兼容指针类型赋值,用于指向struct的指针

时间:2014-12-28 10:40:49

标签: c pointers types compiler-warnings

我对c很新,我正在尝试实现一个链表。我写了这个:

struct List;
typedef struct List* ListRef;
struct List{
        void *data;
        ListRef next;
        ListRef last;
};

ListRef newList(void* headData);
[...]

ListRef append(ListRef list, void* data){

        ListRef newlist = newList(data)

        list->last->next = newList; //here I get a warning
        list->last = newList;  //here I get a warning

        return newList;
}

newList编译时没有警告。 在我得到的评论的两行中:

警告:从不兼容的指针类型

分配

我做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

更改

    list->last->next = newList; //here I get a warning
    list->last = newList;  //here I get a warning

    return newList;

    list->last->next = newlist; //here I get a warning
    list->last = newlist;  //here I get a warning

    return newlist;

分析:

您有一个名为newList的函数,它与您创建的结构实例(newlist)混在一起。

你肯定不会像你是C的新手那样编码:) 希望这有帮助