1 / 25

第十一章 結構

第十一章 結構. 本章學習重點. 何謂結構 結構變數 結構陣列 巢狀結構 結構與函數 結構與指標 聯合型態. 11-1 何謂結構. 陣列 是數個 相同基本型態 的元素集合 結構 是數個 不同基本型態 的元素集合 結合數個彼此相關的變數在一個名稱之下,而且可以包含許多不同資料型態的變數。結構宣告如下:. struct 結構型態 { 資料型態 1 結構成員 1 ; 資料型態 2 結構成員 2 ; 資料型態 3 結構成員 3 ; : : 資料型態 N 結構成員 N; };.

Download Presentation

第十一章 結構

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第十一章 結構

  2. 本章學習重點 • 何謂結構 • 結構變數 • 結構陣列 • 巢狀結構 • 結構與函數 • 結構與指標 • 聯合型態

  3. 11-1 何謂結構 • 陣列是數個相同基本型態的元素集合 • 結構是數個不同基本型態的元素集合 • 結合數個彼此相關的變數在一個名稱之下,而且可以包含許多不同資料型態的變數。結構宣告如下: • struct 結構型態 • { • 資料型態1 結構成員1 ; • 資料型態2 結構成員2 ; • 資料型態3 結構成員3 ; • : : • 資料型態N 結構成員N; • };

  4. 11-2 結構變數 • 使用結構型態來宣告變數 • 分離型宣告 先宣告結構的組成內容,以建立一個新的型態,然後再把變數宣 告成該型態。如下所示: • 1 struct family • 2 { • 3 char ch; • 4 int age; • }; • 6 • 7 struct family show; /*宣告show為family型態的變數名稱 */

  5. 範例 Ch11_1 (1/2) Ch11_1 分離型宣告 1 #include<stdio.h> 2 void main( ) 3 { 4 struct family /*此結構型態名稱為family */ 5 { 6 char name[20]; 7 int age; 8 }; 9 struct family people; /*結構型態family的變數名稱為people */ 10 strcpy(people.name, “John Wang”); /*指定結構的初始值 */ 11 people.age = 18; 12 printf(“Name = %s\n”, people.name); /* people.name,指出people的成員 */ 13 printf(“Age = %d”, people.age); /* people.age 以『.』指出people的成員 */ 14 }

  6. Ch11_1 輸出結果 (2/2) • 程式執行結果 • Name = John Wang • Age = 18

  7. 11-2 結構變數 2. 結合型宣告 此種宣告方式是把分離型宣告的兩個階段合而為一。如下所示: 1 struct family 2 { 3 char ch; 4 int age; 5 } people;/*宣告people為family型態的變數名稱 */

  8. 範例 Ch11_2 (1/2) Ch11_2 結合型宣告 1 #include<stdio.h> 2 void main( ) 3 { 4 struct family /*此結構型態名稱為family */ 5 { 6 char name[20]; 7 int age; 8 }people; /*結構型態family的變數名稱為people */ 9 strcpy(people.name, “John Wang”); /*指定結構的初始值 */ 10 people.age = 18; 11 printf(“Name = %s\n”, people.name); /* people.name,指出people的成員 */ 12 printf(“Age = %d”, people.age); /* people.age 以『.』指出people的成員 */ 13 }

  9. Ch11_2 輸出結果 (2/2) • 程式執行結果 • Name = John Wang • Age = 18

  10. 11-3 結構陣列(1/2) • 結構也可以組成陣列,其語法如下: 例如: struct family people[2] 表示在一結構中,其型態為family,此family有一名稱為people[2]的陣列。元素分別為: people[0] people[1] 此結構若由下頁的宣告而來: struct 結構型態名稱 陣列名稱[元素個數]

  11. 11-3 結構陣列(2/2) struct family { char name[10]; int age; }; struct family people[2] = {{“John”, 18},{“Mary”, 20}} • 則其結構陣列可表示如下:

  12. 範例 Ch11_3 (1/3) Ch11_3 結構陣列 1 #include<stdio.h> 2 void main( ) 3 { 4 struct company 5 { 6 char name[10]; /*宣告結構陣列char name[10],然後 7 int salary; 再宣告salary,prize,total為整數型態*/ 8 int prize; 9 int total; 10 };

  13. 範例 Ch11_3 (2/3) 11 struct company person[ ] = {{"Mary", 20000, 1500}, 12 {"Jack", 30000, 2000}, 13 {"John", 15000, 5000}, 14 {"Tony", 25000, 2500}}; 15 int i; 16 for(i = 0; i < 4; i++) 17 { 18 person[i].total = person[i].salary + person[i].prize; 19 printf("%s", person[i].name); 20 printf("%8d", person[i].salary); 21 printf("%8d", person[i].prize); 22 printf("%8d\n", person[i].total); 23 } 24 }

  14. Ch11_3輸出結果 (3/3) • 程式執行結果 • Mary 20000 1500 21500 • Jack 30000 2000 32000 • John 15000 5000 20000 • Tony 25000 2500 27500

  15. 11-4 巢狀結構 • 結構中可以包含結構,而形成一個巢狀結構。如下所示: struct person { char name[20]; int age; }  struct family /*結構family中包含結構person*/ { struct person brother; struct person sister; } struct family group = {{“Tony”, 22}, {“Mary”, 20}}; 則 group.brother.name = Tony group.brother.age = 22 group.sister.name = Mary group.sister.age = 20

  16. 範例 Ch11_4 (1/2) Ch11_4 巢狀結構 1 #include<stdio.h> 2 main() 3 { 4 struct company 5 { 6 int salary; 7 int prize; 8 }; 9 struct class 10 { 11 struct company person; 12 int total; 13 }p;

  17. 範例 Ch11_4 (2/2) • p.person.salary = 28000; • p.person.prize = 7000; • p.total = p.person.salary + p.person.prize; • printf("total = %d\n", p.total); 18 } • 程式執行結果 • total = 35000

  18. 11-5 結構與函數

  19. 範例 Ch11_5 (1/2) Ch11_5 結構與函數之傳值法 1 #include<stdio.h> 2 struct add { 3 int a, b; 4 }number; 5 main() 6 { 7 int total; 8 static struct add number = {50, 70}; 9 total = sum(number.a, number.b); 10 printf("The result : a + b = %d\n", total); 11 } 12 sum(int m, int n) 13 { 14 return(m + n); 15 }

  20. Ch11_5 輸出結果 (2/2) • 程式執行結果 • The result : a + b = 120

  21. 範例 Ch11_6 (1/2) Ch11_6 結構與函數之傳址法 1 #include<stdio.h> 2 struct add { 3 int a, b; 4 }number; 5 main() 6 { 7 int total; 8 static struct add number = {50, 70}; 9 total = sum(&number); 10 printf("The result : a + b = %d\n", total); 11 } 12 sum(ab) 13 struct add *ab; 14 { 15 return(ab -> a + ab -> b); 16 }

  22. Ch11_6 輸出結果 (2/2) • 程式執行結果 • The result : a + b = 120

  23. 11-7 聯合型態 • 聯合(unions)型態是將不同型態的資料存放在同一記憶體的空間內 • 前面提到的結構是將每一種不同型態的資料存放在不同記憶體的空間內。 • 結構中的資料型態必須佔用不同的記憶體空間,而聯合則可以使不同的資料型態佔用相同的記憶體空間。 • union 聯合名稱 • { • 資料型態 聯合項目; • 資料型態 聯合項目; • 資料型態 聯合項目; • }聯合變數; • union class • { • int i; • char *name; • char *number • }student;

  24. 範例 Ch11_8 (1/2) Ch11_8 聯合型態 • #include<stdio.h> • void main() • { • struct class • { • int i; • char c; • }; • union score • { • float b; • struct class student; • };

  25. 範例 Ch11_8 (2/2) • union score a; • printf(“size = %d\n”, sizeof(union score)); • printf(“a.student.i = %d\n”, a.student.i = 75); • printf(“a.student.c = %c\n”, a.student.c = ‘K’); • printf(“a.b = %.1f\n”, a.b = 60.0); 19 } • 程式執行結果 • size = 4 • a.student.i = 75 • a.student.c = K • a.b = 60.0

More Related