1 typedef
作用:起別名
1.1 typedef 變量
typedef int A;
A x = 10;
給 int 起別名,爲 A
1.2 typedef 結構體
typedef struct{
...
}Student;
給 struct{ … } 起別名,爲 Student
另:
- 有typedef,定義變量:
Student stu; - 無typedef,定義變量(加struct):
struct Student stu;
2 結構體
2.1 定義
兩種:
// (1)
typedef struct{
...
}Student, *PStudent;
// (2)
typedef struct Student{ // 由於內容中有struct Student
...
struct Student *deskmate; //與(1)區別
}Student;
2.2 使用
以上兩種均一樣
類型 | 定義變量 | 訪問成員 |
---|---|---|
結構型(定義1個) | Student stu; | stu.a |
結構型(定義n個) | Student stu[n]; | stu[i].a |
指針型(定義1個) | Student *stu; stu = (Student *)malloc(sizeof(Student)); |
stu->a 或 (*stu).a |
指針型(定義n個) | Student *stu; stu = (Student *)malloc(n * sizeof(Student)); |
stu[i]->a 或 (*stu)[i].a |
指針型中:PStudent pstu;
等價 Student *stu;