char name 120 20 char name 220 getsname
- Slides: 28
複習 char name 1[20]; //長度為 20的字串 char name 2[20]; //讀取字串 gets(name 1); fflush(stdin); scanf(“%d”, name 2); //列印 puts(name 1); //會自動加上換行 printf(“%sn”, name 2);
複習 //處理字串的函數 If(!strcmp(name 1, name 2)){ //name 1和name 2一樣時會執行 } strcat(name 1, name 2); //將name 2接在name 1 //的後面 strcpy(name 1, name 2); //將name 2的內容 //copy到name 1
複習 char ch; //讀取字元 ch=getchar(); ch=getch(); scanf(“%c”, &ch); //印出字元 putchar(ch); putch(ch); printf(“%c”, ch);
Class 7 結構
範例 l struct employee_data_type { char name[10], gender; int salary; }; //注意這裡要加分號 //定義結構部份 l struct employee_data_type employee; //employee才是個變數
結構定義 l structure_tag // structure_tag 是個結構 的名字 { data members declarations; }; //註: structure_tag, member_name, variable_name 可以是相同的 l l l struct my. Type{ int my. Type; }my. Type;
變數宣告 l 直接寫在定義結構的時候 l l l 可以是省略結構定義的structure_tag struct student_type student[10]; struct student_type { int id, class; }student 1[10]; struct { int id, class; }student 2[10]; 像平常宣告變數的情況 l struct employee_data_type employee;
結構的取用方式 l 結構成員運算子(. ) //variable. member_name; l printf("The employee's name is %s. n", employee. name); l printf("The employee's salary is %d. n", employee. salary); l 結構指標成員運算子(->) l 如果取用的是一個結構指標變數,可以使用(->)運算子。 l struct employee_data_type *ptr_employee=&employee; printf("The employee's name is %s. n", (*ptr_employee). name); printf("The employee's name is %s. n", ptr_employee->name); printf("The employee's name is %s. n", (&employee)->name); //兩種取用的方式是一樣的,由於成員運算子 (. )/(->) 的結合優先權 比提領/取址運算子 (*)/(&) 還高,所以要記得用 () 括起來。
結構陣列 l 宣告 l l 字串輸入 l l employee 2[0]. salary=1000; 字串輸出 l l strcpy(employee 2[0]. name, "Mary"); 整數輸入 l l struct employee_data_type employee 2[5]; printf("The employee's name is %s. n", employee 2[0]. name); 整數輸出 l printf("The employee's salary is %d. n", employee 2[0]. salary);
巢狀結構 l 結構可以不只一層,想要描述複雜的事物,我們可以用雙層以上的結構 來描述。 l 巢狀部件 1: l l 巢狀部件 2: l l struct body_type { char color; int num_doors; }; 巢狀主體: l l struct engine_type { int capacity; char num_cylinders; }; struct car_type { struct engine_type engine; struct body_type body; } car; 取用方式相似: l l car. engine. capacity=1500; car. body. num_doors = 4;
簡化struct的定義語法-typedef l l 用來改變一個資料型態的名稱、或是簡化struct資料型態的名稱 Case 1 如何把原來的宣告方式加上一個新的"暱稱"? l struct employee_data_type //已經先宣告好了 { char name[10], gender; int salary; }; l typedef struct employee_data_type EMPLOYEE; //加這個typedef l EMPLOYEE employee={"Yang", 'M', 20000}; l EMPLOYEE employee 2; //以後拿EMPLOYEE來定義就可以, //當然還是可以繼續使用struct employee_data_type來定義。
typedef l Case 2 可以像以下這種寫法: l typedef struct { char name[10], gender; int salary; }EMPLOYEE; //這樣跟Case 1定義出EMPLOYEE的方式一樣, //不過系統就不知道struct employee_data_type。
enum l 定義一些有彼此有相關性的常數 enum COLOR { BLUE, YELLOW, RED, PINK }; l l 這樣其實四個常數會對應到 0、1、2、3,但是寫程式時不必特別記得 例如在程式中可以這樣寫: COLOR color = YELLOW; switch(color) { case BLUE: printf("藍色n"); break; case YELLOW: printf("黃色n"); break; case RED: printf("紅色n"); break; case PINK: printf("粉紅色n"); break; default: printf("不知道n"); break; }
malloc+struct l l l l struct my. Type{ int a; int b; }; int main(){ my. Type *s; int size=5; l s=(my. Type*)malloc(sizeof(my. Type)*size); s->a=99; s->b=80; printf("%d %d", s->a, s->b); system("pause"); l l l }
- 120*120 px
- 140+120+120
- Const char *s =
- Const char * vs char * const
- Char name 25 charlanguage 10
- Char name[20]
- Const char * const *
- Double grade
- Struct student
- Struct student char *name
- Char name 20
- Name a point that is collinear with the given points
- Pound to kg equation
- Animated calculator
- Definer 220
- Ms&e 220
- Uiuc ece 220
- Cse 220
- Dice one
- Georg cantor kümeler kuramı
- 220-901 questions answers
- Tentukan semua balikan dari 9 (mod 11)
- Jak obliczyć stężenie procentowe
- Smtp 220
- Sin i / sin r = n
- Ece 220 wiki
- Arkani-hamed
- Karvonen formule
- Target heart rate zone