我正在爲我的基本C分配製作鏈表程序。 但是,我總會在.exe上得到強制關閉錯誤,並在Ubuntu上得到分段錯誤。簡單鏈接列表C代碼分段錯誤
我試圖分解並重寫,但我不知道代碼失敗的地方。
我很感謝您的幫助。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node{
char name[20];
int mark;
struct node *next;
};
struct node *addnode(char name[], float mark);
int main(void){
int j = 0;
char StdName[10];
float StdMarks;
struct node *head = NULL;
struct node *curr = NULL;
head = curr = addnode('\0',0.0);
for(j=0; j<3; j++){
printf("\nEnter StdName >>");
printf("\nMarks for %s >>", StdName);
curr -> next = addnode("", 5.5);
curr = curr->next;
}
curr = head -> next;
j = 0;
printf("\nnode\tName\tMarks");
while(curr){
printf("\n%d\t%s\t%5.2f", j++, curr->name, curr->mark);
curr=curr->next;
}
return 0;
}
struct node *addnode(char name[], float mark){
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
strcpy(temp->name,name);
temp->mark=mark;
temp->next=NULL;
return (temp);
}
考慮使用調試器?嘗試尋找'gdb' – axiom