ฉันได้กำหนดโครงสร้าง "รถยนต์" ด้วยโมเดล (char *model) และปีของโมเดล (int year) ฉันมีฟังก์ชั่นที่จะสร้างโครงสร้างรถใหม่ อย่างไรก็ตาม มันเป็นข้อผิดพลาด seg เมื่อคัดลอกตัวชี้ถ่าน นี่ควรจะสร้างโหนดใหม่สำหรับรายการที่เชื่อมโยง
Car *newCar(char *model, int year){
Car *new = malloc(sizeof(Car));
new->year = year;
new->model = malloc(MAX_LENGTH*sizeof(char));
strcpy(new->model, model);
new->next = NULL;
return new;
}
new->model = malloc(strlen(model) + 1)
ล่ะ? - person cnicutar   schedule 11.03.2013char *model
ไม่ใช่NULL
นอกจากนี้ ตามแนวทางปฏิบัติที่ดี ควรตรวจสอบการส่งคืนของmalloc
s เสมอ - person congusbongus   schedule 11.03.2013