1 / 17

第 14 章 結構與其他資料形式

第 14 章 結構與其他資料形式. 問題範例:建立書籍目錄. 範例 14.1 book.c. 建立結構宣告. struct book { char title[MAXTITL]; char author[MAXAUTL]; float value; } ;. 定義結構變數. struct book libry; 表示 libry 是一個 book 結構的變數. 取得結構成員的存取. 使用點 (.) 運算子 gets(libry.title); gets(libry.author);. 結構陣列.

Mia_John
Download Presentation

第 14 章 結構與其他資料形式

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. 第14章 結構與其他資料形式

  2. 問題範例:建立書籍目錄 • 範例14.1 book.c

  3. 建立結構宣告 struct book { char title[MAXTITL]; char author[MAXAUTL]; float value; };

  4. 定義結構變數 • struct book libry;表示libry是一個book結構的變數

  5. 取得結構成員的存取 • 使用點(.)運算子gets(libry.title);gets(libry.author);

  6. 結構陣列 • 範例14.2 manybook.c struct book libry[MAXBKS];

  7. libry[0] libry[0].title libry[0].author libry[0].value libry[1] libry[1].title libry[1].author libry[1].value libry[2] libry[2].title libry[2].author libry[2].value libry[99] libry[99].title libry[99].author libry[99].value char array[40] char array[40] float type

  8. 識別結構陣列的成員 • 在結構名稱後使用點運算子,之後接著成員名稱libry[0].authorlibry[0].value • libry[2].title[4]表示第三個結構所描述之書籍標題的第五個字元

  9. 巢狀結構 • 範例14.3 • 存取巢狀結構的成員,此時要使用二次的點運算子。printf("Dear%s,\n\n",fellow.handle.first);

  10. 指向結構的指標 • 範例14.4 • 宣告與初始結構指標struct guy * him; • 使用指標存取成員him->income(*him).income)

  11. 使用結構位址範例14.6 funds2.c sum(&stan)使用原始結構的資料 • 傳遞結構作為參數範例14.7 funds3.c sum(stan)以原結構的複本來執行

  12. 更多結構的特徵 • 範例14.8 names1.c • 範例14.9 names2.c • 結構或是指向結構的指標?P645

  13. 結構中的字元陣列或字元指標字元指標,並沒有配置空間來儲存字串,它只能用於已經配置了空間的地方,像是陣列中的字串。結構中的字元陣列或字元指標字元指標,並沒有配置空間來儲存字串,它只能用於已經配置了空間的地方,像是陣列中的字串。

  14. 結構、指標與malloc()函數 • 在結構中使用指標來處理字串時,一種有意義的方法是使用malloc()函數來配置記憶土並只用指標來儲存位址,優點是可使用malloc()函數來配置恰好需要字串儲存空間。 • 範例14.10 names2.cgetinfo()函數會將數入讀到暫時的陣列中,並使用malloc()函數來配置儲存空間,然後將字串複製到新配置的空間。這兩個字串都不是存在結構中,它們被儲存在以malloc()函數所配置的記憶體區塊裡。

  15. 使用結構陣列的函數 • 陣列名稱就等於它的位址,這個陣列名稱可以被傳遞給函數。 • 範例14.11 funds4.c

  16. 鏈結串列 2240 2240 Modern Times 10 2360 前端 標題 評分 下一個 2360 Titanic 8 2100 標題 評分 下一個 2100 Titanic 8 NULL 標題 評分 下一個 有很多項目的鏈結串列

  17. 範例14.14 • 顯示串列 • 建立串列 • 清空串列

More Related