【发布时间】:2015-09-13 17:31:53
【问题描述】:
我想写一个小函数来释放链表的内存。已使用 malloc 创建节点。
一个典型的函数是:
void freeList(struct node* head)
{
struct node* tmp;
while (head != NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
}
我的问题是我收到以下错误,我不知道如何解决它们:
警告:来自不兼容指针类型的赋值 tmp = l;
错误:“linked_list”没有名为“next”的成员 l = l -> next;
声明如下:
struct node_s {
struct node_s *next;
char msg[MAX_MSG_LEN];
unsigned int time;
};
typedef struct node_s node;
struct linked_list_s {
node *head;
};
typedef struct linked_list_s linked_list;
void list_free(linked_list *l) {
struct node *tmp;
while (l !=NULL) {
tmp = l;
l = l -> next;
free(tmp);
}
}
【问题讨论】:
标签: c memory linked-list malloc