[英]C segmentation fault linked list
對於學校,我必須對鏈接列表實現彈出功能。 但是,當我運行它時,我遇到了分段錯誤,我也不知道為什么……我已經調試了它,並指出* value = current-> next; 我是C的新手,如果這是一個愚蠢的問題,我們感到抱歉。
注意:除了pop之外,我還必須實現更多功能。 因此,您在此處看到的所有其他函數調用均已實現並且可以正常工作。
list.h
int list_pop(struct List* list, int* value);
list.c
int list_pop(struct List* list, int* value)
{
struct ListNode* curr = list->first;
struct ListNode* prev;
if (curr == NULL)
return 0;
while (curr != NULL)
{
prev = curr;
curr = curr->next;
}
*value = curr->value; **Debugger says that in this line there is something wrong.**
prev->next = NULL;
free(curr);
return 1;
預先謝謝您,如果有人可以告訴我此彈出功能是否正確:)也會非常酷:)
while (current != NULL)
{
previous = current;
current = current->next;
}
*value = current->value;
在while循環結束時,current為NULL。
您想在存在下一個元素時進行迭代。 因此,您應將其替換為
while (current->next != NULL)
while (current != NULL)
因此,當current
為NULL時,此while循環將終止。 精彩! 現在,接下來會發生什么?
*value = current->value;
好吧,當前為NULL。 取消引用NULL指針會使調試器非常難過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.