我正在構建一個學生管理系統,它必須使用鏈表。要求之一是每個學生都必須有一個他們注冊的課程串列。為了處理這個問題,我實作了 2 個單獨的鏈表,然后在必要時加入它們。但是在執行該printList函式時,只有最后輸入的學生得到輸出,然后出現上述錯誤代碼。此外,我嘗試在在線編譯器中運行代碼并且運行良好,任何幫助將不勝感激。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Structures creation
typedef struct course {
char courseName[40];
int credits;
struct course *nextCourse;
} course;
typedef struct student {
char firstName[50];
char lastName[50];
int age;
char address[100];
char progName[60];
course *courseList;
struct student *next;
} student;
void *checkMalloc(size_t num_bytes);
void addStudent(student **headStd, char *fName, char *lName, int age, char *address, char *progName);
void addCourse(student *currStd, char *cName, int cred);
void printList(student *first);
int main() {
student *headStd = NULL;
addStudent(&headStd, "John", "Doe", 19, "Lot 123", "Comp Sci");
addCourse(headStd, "CSE2100", 4);
addCourse(headStd, "CSE2101", 4);
addStudent(&headStd, "Jane", "Smith", 20, "Lot 3 East Street", "Info Sys");
addCourse(headStd, "ISY2100", 4);
addCourse(headStd, "ITE2101", 3);
printList(headStd);
return EXIT_SUCCESS;
}
void addStudent(student **headStd, char *fName, char *lName, int age, char *address, char *progName) {
student *newStd = checkMalloc(sizeof(*newStd));
strcpy(newStd->firstName, fName);
strcpy(newStd->lastName, lName);
newStd->age = age;
strcpy(newStd->address, address);
strcpy(newStd->progName, progName);
// This line was missing in the original code posted
// newStd->courseList = NULL;
newStd->next = (*headStd);
(*headStd) = newStd;
}
void addCourse(student *currStd, char *cName, int cred) {
//Memory allocation
course *newCourse = checkMalloc(sizeof(*newCourse));
strcpy(newCourse->courseName, cName);
newCourse->credits = cred;
newCourse->nextCourse = currStd->courseList;
currStd->courseList = newCourse;
}
void printList(student *first) {
while (first != NULL) {
printf("Name: %s %s\n", first->firstName, first->lastName);
printf("Age: %d\n", first->age);
printf("Address: %s\n", first->address);
printf("Program Enrolled: %s\n", first->progName);
course *currCourse = first->courseList;
if (currCourse == NULL) {
printf("\tNo Courses Present\n");
} else {
while (currCourse != NULL) {
printf("\tCourse Name: %s\n", currCourse->courseName);
printf("\tCourse Credits: %d\n", currCourse->credits);
currCourse = currCourse->nextCourse;
}
}
first = first->next;
}
}
void *checkMalloc(size_t num_bytes) {
void *memory = malloc(num_bytes);
if (memory == NULL) {
printf("Error, couldn't allocate memory.\n");
exit(-1);
}
return memory;
}
uj5u.com熱心網友回復:
在您發布的代碼中,該成員newStd->courseList未由函式初始化addStudent。由于記憶體是用 分配的malloc,它的內容是不確定的,這意味著該指標courseList可能是一個無效的指標。
然后你在這個無效指標之前插入一個課程串列......第二個學生也會發生同樣的情況......那里的無效指標沒有被取消參考,而是像毒蛇一樣等待咬人。
當您列印學生詳細資訊和課程串列時,在最后一門課程之后,該行將currCourse = currCourse->nextCourse;無效指標讀入,currCourse并且在取消參考此指標時下一次迭代將崩潰。
這解釋了觀察到的行為,但不能保證這種未定義的行為是可觀察到的,因為它malloc也可能回傳恰好被所有位填充為零的記憶體,因此 的初始值newStd->courseList可能是空指標。
解決方法是簡單的:加newStd->courseList = NULL;在addStudent()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/400698.html
