C中指针赋值后出现分段错误



有代码:

void pop(int id, List *head) {
List **previous = head;
List **current = (*previous) -> next;
if(head -> next == NULL && (*head).id == id) {
free(head);
} //if only 1 node in List
while((**current).id != id) {
*previous = head -> next;
*current = previous;
}
(*previous) -> next = (*current) -> next;
free(*current);
}

程序落在第三个笔划List **current = (*previous) -> next上的segmentation fault(我用printfs检查了它(。

CLion调试器称Exception: EXC_BAD_ACCESS (code=1, address=0x113d349e2)

如果我做printf("address = %dn", &((*previous) -> next));,它会给我地址。

void delete_worker_by_id(int id, List *head) {
List **previous = &head;
List *current = (*previous) -> next;
if(head -> next == NULL && (*head).id == id) {
free(head);
head = NULL;
} //if only 1 node in List
while((*current).id != id) {
*previous = current;
current = current -> next;
}
if((*current).id == id) {
(*previous) -> next = current -> next;
current -> next = NULL;
free(current);
return;
}
puts("No worker with this id");
}

相关内容

  • 没有找到相关文章

最新更新