1

在下面的代码摘录中,它给了我警告:从不兼容的指针类型分配给指出的两行。我究竟做错了什么?

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

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

4

1 回答 1

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:我个人认为typedefs很烂,最好谨慎使用

于 2013-10-04T09:37:26.007 回答