更改链接列表中指针地址的问题

时间:2013-10-04 09:33:17

标签: c pointers linked-list

在下面的代码中,它为我提供警告:从不兼容的指针类型分配两条线。我做错了什么?

typedef struct {
    char* string;
    struct samplelist* nextchunk;
    struct samplelist* prevchunk;
} samplelist;

samplelist* startsamplelist;
samplelist* lastsamplelist;


samplelist* newchunk = checked_malloc(sizeof(samplelist));

lastsamplelist->nextchunk = newchunk; //warning here
newchunk->prevchunk = lastsamplelist; // warning here
lastsamplelist = newchunk; //no problem here though

编辑:附上相关定义,下面的答案不依赖于添加的代码。是语法错误

1 个答案:

答案 0 :(得分:1)

问题在于:您需要在samplelist之后的第一行添加符号struct

typedef struct samplelist {
    char* string;
    struct samplelist* nextchunk;
    struct samplelist* prevchunk;
} samplelist;

一般来说,可能是:

typedef struct foobar {
    char* string;
    struct foobar* nextchunk;
    struct foobar* prevchunk;
} samplelist;

ps:我个人认为typedef很糟糕,最好用得很少