【发布时间】:2011-05-02 15:01:00
【问题描述】:
我仍在尝试掌握 C 中的指针、结构和 malloc。我正在尝试使用链表来实现哈希表。当我尝试编译时,我从不兼容的指针类型错误中得到返回:
struct Mlist_head{
struct Mlist_node *head;
struct Mlist_node *tail;
};
struct MList {
int size;
struct Mlist_head hashtable[HASHSIZE];
};
MList *ml_create(void){
struct MList *m;
struct Mlist_head *h;
int i;
if ((m = (struct MList *)malloc(sizeof(struct MList))) != NULL){
if ((h = (struct Mlist_head *)malloc(sizeof(struct Mlist_head))) != NULL) {
for (i = 0; i < HASHSIZE; i++) {
h = &(m->hashtable[i]);
h->head = NULL;
h->tail = NULL;
}
printf("worked");
return m;
}
}
}
我确定这里可能还有其他错误(可能是语义错误),但一次只有一件事:)
感谢您的帮助
【问题讨论】:
-
是的,“其他错误”之一是如果分配失败,您根本不会返回任何内容。