[英]c - Segmentation Fault Linked List
我正在做一個有關C語言中鏈表的項目的學習。雖然我的代碼確實可以編譯,但是在嘗試運行我的代碼時遇到了段錯誤。 我知道細分錯誤發生的位置,但是我試圖找出問題所在。 這是我的代碼:
//
// It reads the list from the file_name indicated. If the list already has entries,
// it will clear the entries.
//
int llist_read(LinkedList * list, char * file_name) {
//Still Need to complete this!!!
ListNode * e;
int val;
FILE *f;
e = list->head;
while (e != NULL) {
e->value = 0;
e = e->next;
}
e = list->head;
if (file_name == NULL) {
return 0;
}
if ((f = fopen(file_name, "r")) == NULL) {
return 0;
}
if (fscanf(f, "%d\n", &val, val) == 0) {
return 0;
}
else {
e->value = val; //Segmentation Fault occurs here.
e = e->next;
e = fscanf(f, "%d", e->value);
while (e->next = fscanf(f, "%d", e->value) != EOF) {
}
}
return 1;
}
我在這里遇到細分錯誤:
e->value = val; //Segmentation Fault occurs here.
任何關於為什么它不起作用的建議將不勝感激!
在將值分配給e->value
,應檢查是否有分配給e
內存。 將賦值語句替換為:
if ( e )
{
e->value = val;
e = e->next;
fscanf ( f, "%d", &(e->value) );
}
您還必須在此if
語句中解決循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.