[1] 不用typedef時的結構體形式:
struct student
{
int age;
int score;
}stu,*pstu;
這樣的語句的含義簡單明瞭,就是定義了一個結構體,同時創建了兩個student結構的變量stu和*pstu。
訪問內部元素方式:
stu.age=1;
pstu->age=1;
(*pstu).age=1;
[2] 比較好用的形式:typedef寫在前頭
typedef struct student
{
int age;
int score;
}STU,*PSTU;
此時STU和PSTU已經不是變量名了而是一個類型。
其中STU是struct類型,而PSTU則是struct *類型。舉個例子來對比一下:
typedef int DATA;
typedef int* ADDRESS;
沒錯,就像我們給int一個別名DATA、給int*一個別名ADDRESS一樣,student結構體也擁有了別名STU,student * 別名PSTU
使用的時候和使用類型一樣:
STU peter;//DATA a;
PSTU p=&peter;//ADDRESS p=&a;
一定不要把後邊的STU和*PSTU當做變量名[3] 更清晰的定義方式
其實2中的使用方式是下面方法的簡化版本:
struct student
{
int age;
int score;
};
typedef struct student ST;
typedef struct student* PST;
(當然現在已經可以不用寫struct直接用student來定義結構體變量了,例如typedef student STUD;)