1 / 15

第十一章

第十一章. 各式各樣的資料型態. 11-1 結構的基礎知識. 決定新的型態 關於結構 結構型態的宣告 struct  結構型態 { 資料型態 識別字 ; 資料型態 識別字 ; …. 11-1 結構的基礎知識. 宣告結構變數 結構變數的宣告 結構型態 結構變數名稱 ; struct Car car1;. 11-1 結構的基礎知識. 對成員進行存取 存取結構型態的成員 結構變數名稱 . 成員 car1.num = 1234; car1.gas = 25.5;. 11-2 結構的寫法. 利用 typedef 來分割名稱 typedef

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. 11-1 結構的基礎知識 • 決定新的型態 • 關於結構 結構型態的宣告 struct 結構型態 { 資料型態 識別字; 資料型態 識別字; …

  3. 11-1 結構的基礎知識 • 宣告結構變數 • 結構變數的宣告 結構型態 結構變數名稱; struct Car car1;

  4. 11-1 結構的基礎知識 • 對成員進行存取 • 存取結構型態的成員 結構變數名稱.成員 car1.num = 1234; car1.gas = 25.5;

  5. 11-2 結構的寫法 • 利用typedef來分割名稱 • typedef typedef 資料型態  識別字; /* 結構型態struct Car的宣告 */ typedef struct Car{ int num; double gas; } Car;

  6. 11-2 結構的寫法 • 結構的初始化 • 結構的初始化 結構型態 結構變數名稱 = {值, 值, …}; Car car1 = {1234, 25.5};

  7. 11-2 結構的寫法 • 把值指定給結構 • car2 = car1; • 對結構進行指定後,成員的值會逐項複製儲存至所指定的目的地。 • 對結構進行指定後,就會把值儲存至各成員。

  8. 11-3 結構的大小 • 理解結構型態的大小 • 結構的大小不一定是成員大小的加總。同時,指向大型結構的指標,通常會小於該資料型態的大小

  9. 11-3 結構的大小 • 使用位元欄(bit field) • 位元欄 struct 結構型態名稱{ 資料型態 識別字 : 位元數; 資料型態 識別字 : 位元數; … };

  10. 11-4 結構的應用 • 把結構當做引數來用 • 把結構當作函數的引數來用,各成員便會被複製傳送。

  11. 11-4 結構的應用 • 把指向結構的指標當作引數來用 • 把指向結構的指標拿來當作函數的引數,所傳送的就是位址。 • 欲從指向結構的指標存取成員時,使用成員運算子(->)會很方便。

  12. 11-4 結構的應用 • 製作結構的陣列 Car cars[3]; int i; cars[0].num = 1234; cars[0].gas = 25.5; cars[1].num = 4567; cars[1].gas = 52.2; cars[2].num = 7890; cars[2].gas = 20.5;

  13. 11-5 共同空間 • 關於共同空間 • 共同空間型態的宣告 union 共同空間型態名稱{ 資料型態 識別字; 資料型態 識別字; … };

  14. 11-6 列舉型態 • 關於列舉型態 • 列舉型態的宣告 enum 列舉型態名稱 {識別字1, 識別字2, 識別字3, …} • 指定列舉型態的數值 typedef enum Week{SUN, MON, TUE, WED, THU, FRI, SAT} Week;

  15. 綜合整理 • 本章學習過的內容與重點 • C語言除了基本資料型態以外,還可以製作新的資料型態。 • 結構可以整合不同資料型態的值。 • 欲存取結構的成員時,使用成員選擇運算子(.)。 • 使用typedef可以命名獨自的資料型態名稱。 • 可以對結構指定同樣型態的結構。 • 結構可以使用位元欄的功能。 • 從結構的指標存取成員時,使用成員選擇運算子(->)相當方便。 • C語言可以製作結構的陣列。 • 共同空間的各成員,共同擁有相同的記憶體。 • 列舉可以儲存識別字的值。

More Related