我是編程新手。所以這裏的問題是,當我想執行下面的程序,但它只是顯示程序已停止工作。我不知道什麼是什麼代碼是錯誤的,因爲沒有編譯錯誤。任何幫助將不勝感激。嘗試運行鏈表c程序時出錯
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct ppl
{
char name[30];
struct ppl *next;
};
main()
{
struct ppl *student1, *student2, *student3, *student4, *temp, *ptr, *x;
struct ppl *head = NULL;
int no;
head = (struct ppl *)malloc(sizeof(struct ppl));
student1 = (struct ppl *)malloc(sizeof(struct ppl));
student2 = (struct ppl *)malloc(sizeof(struct ppl));
student3 = (struct ppl *)malloc(sizeof(struct ppl));
student4 = (struct ppl *)malloc(sizeof(struct ppl));
head->next = student1;
strcpy(student1->name, "Aizar");
student1->next = student2;
strcpy(student2->name, "Chandi");
student2->next = student3;
strcpy(student3->name, "Faizul");
student3->next = student4;
strcpy(student4->name, "Joshua");
student4->next = NULL;
ptr = head;
while (ptr->next != NULL)
{
ptr = (struct ppl *)malloc(sizeof(struct ppl));
ptr = ptr->next;
printf("Name list : %s \n", ptr);
};
return 0;
}
您是否嘗試過使用調試器帳戶? – user4098326 2015-04-02 13:32:24
我不認爲你打算在while循環內部進行分配。一旦你調用malloc(在while內部),你已經完全在列表之外完成了ptr。 – 2015-04-02 13:34:01
你想從這個程序中做什麼?打印學生姓名? – MCHAppy 2015-04-02 13:35:56