列舉
把一些數值用字串表示, 方便記憶, 並設定某個變數只能是這個列舉中的值. 這個型態比較聰明, 會自動幫使用者累加
enum Color{
Red=1,
Green,
Blue
};
enum Color pen;
pen=Blue;
printf("%d \n", pen);
pen 只能設定為Red, Green, Blue其中一種而己,其他都不行
精簡法
enum Color{
Black,
Red=5,
Green,
Blue
}pen=Blue, pencil=Black;
Black為0, Green為6, Blue 為7
結構
struct 為自訂資料型態, 集合各個不同的資料為一種新的資料型態
宣告 :
printf(“%s\n%d\n%d\n”, thomas.name, thomas.math, thomas.chinese);//使用 “.” 存取
精簡
struct Student{
char name[10];
int math;
int chinese;
}thomas, john={"john", 50,100};;
thomas.name= “Thomas”;//error, 因為此陣列己指定給10byte的空間,不能再移位,只能用strcpy(thomas.name, “Thomas”);
另類初始值
struct Student{
char name[10]="default";
int math=0;
int chinese=0;
}thomas;//以上ok,可以初始化
但
struct Student{
char name[10]="default";
int math=0;
int chinese=0;
}thomas{"Thomas", 90, 100};//一定掛掉, 需一個一個指定 thomas.math=100;
另人抓狂但又需記住的東西
struct Student{
char name[10];
int math;
int chinese;
}a;
Student *thomas=&a;//宣告為指標,並指向己宣告的struct, 此時需用 -> 來存取,不能用 "."
strcpy(thomas->name, "Thomas");
thomas->math=100;
等等,為什麼要用 Student *thomas=&a ?? 如果用Student thomas=a可以嗎!!
Ans : 可以,此時就會由a複製一個新的結構給thomas, 二者完全獨立
新生物件
Student *thomas=new Student();產生一個新的結構, 物件觀念開始成形了
