這幾天看代碼,看到幾種類型的結構體,結構聲明如下:
struct book{
string name;
int price;
int num;
};
此種結構定義結構變量的格式如下:
struct book student;
struct book{
string name;
int price;
int num;
}student;
此種形式代表聲明結構的過程和定義結構變量的過程被合併成一步
typedef struct (book){ //book可省略
string name;
int price;
int num;
}student;
typedef的作用是爲一個已存在的類型創建一個名字,故此種結構的結構體定義變量的格式是:student a;
個人認爲最後一種在書寫上比較方便,當你在程序中大量定義結構體變量時。