c/cpp 復健中...
gcc/g++ -o 要生成執行檔名 執行檔名
切換至 cmd compile
科目 | 實體參考書 | OCW |
---|---|---|
DataStructure | 洪逸、王志強 | 彭文志 |
Algorithm | 林立宇 | 江蕙如老師 |
struct Student {
char name[50];
int score;
struct Student *next;
};
struct Student* createStudent(char name[], int score) {
struct Student* newStudent = (struct Student*)malloc(sizeof(struct Student));
if (newStudent == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
strcpy(newStudent->name, name);
newStudent->score = score;
newStudent->next = NULL;
return newStudent;
}
我之前對於 *next
跟struct Student*
有些誤解 最近才搞清楚
白話一點解釋就是struct Student*
是指向struct Student
這個結構的記憶體位置
*next
是指向struct Student
結構中下一個位置的指針
(struct Student*)malloc(sizeof(struct Student))
malloc 前面是定義 return 大小的資料型態
c 語言 function 前面的是定義資料的型態