1 / 29

Chapter 4

Chapter 4. 陣列、指標和參數. 指標陣列 - 以 char 型態為例. 每個元素都是一個指標 每個指標所指的字串長度可以不同 可適用宣告陣列的方式來宣告指標陣列. Ex04_07.cpp. char* pstr[ ]= { "Robert Redford", "Hopalong Cassidy", "Lassie", "Slim Pickens", "Boris Karloff",

woody
Download Presentation

Chapter 4

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. Chapter 4 陣列、指標和參數

  2. 指標陣列-以char型態為例 • 每個元素都是一個指標 • 每個指標所指的字串長度可以不同 • 可適用宣告陣列的方式來宣告指標陣列

  3. Ex04_07.cpp char* pstr[ ]= { "Robert Redford", "Hopalong Cassidy", "Lassie", "Slim Pickens", "Boris Karloff", "Oliver Hardy" };

  4. Figure p149 • 共佔記憶體103bytes

  5. 字串交換 • 第一個字串和第六個字串交換 • 考慮陣列做法 • 考慮指標陣列做法

  6. sizeof 運算子 • 傳回整數常數 • 此常數為運算元所佔byte數 • sizeof無法得知文字字串所佔記憶體大小

  7. Example • pstr為一陣列 • cout << (sizeof pstr) / (sizeof pstr[0]);

  8. Example-常數指標和指向常數的指標 #include <iostream> using namespace std; int main() { char* pstr[] = { "Robert Redford", // Initializing a pointer array "Hopalong Cassidy", "Lassie", "Slim Pickens", "Boris Karloff", "Oliver Hardy" }; char* pmystr="Ruei-Yu Wu"; pstr[0]=pmystr; cout <<"pstr[0]:"<<pstr[0]<<endl; cout <<"pmystr:"<<pmystr<<endl; return 0; }

  9. Example #include <iostream> using namespace std; int main() { const char* pstr[] = { "Robert Redford", // Initializing a pointer array "Hopalong Cassidy", "Lassie", "Slim Pickens", "Boris Karloff", "Oliver Hardy" }; char* pmystr="Ruei-Yu Wu"; pstr[0]=pmystr; cout <<"pstr[0]:"<<pstr[0]<<endl; cout <<"pmystr:"<<pmystr<<endl; return 0; }

  10. pstr[0]=pmystr; pmystr

  11. Example #include <iostream> using namespace std; int main() { const char* const pstr[] = { "Robert Redford", // Initializing a pointer array "Hopalong Cassidy", "Lassie", "Slim Pickens", "Boris Karloff", "Oliver Hardy" }; char* pmystr="Ruei-Yu Wu"; pstr[0]=pmystr; cout <<"pstr[0]:"<<pstr[0]<<endl; cout <<"pmystr:"<<pmystr<<endl; return 0; }

  12. const 相關的指標和物件 • 指標指向常數物件 • 所指物件內容不能改, 指標可指向別的 • const chat* pstring=“Some text”; • 常數指標指向物件 • 所指物件內容可改, 指標所存位址不能改 • chat* const pstring=“Some text”; • 常數指標指向常數物件 • 所指物件內容不可改, 指標所存位址不能改 • const chat* const pstring=“Some text”;

  13. 指標和陣列 • 將陣列第一個元素的位址指定給指標 double* pdata; double = data[5]; pdata=data; • 將陣列第一個元素的位址指定給指標 pdata=&data[1];

  14. 指標算術 • 表示將pdata所儲存的位址+n*sizeof(double) pdata = &data[2]; pdata+=1; • 表示將pdata所儲存的位址+n*sizeof(double) *(pdata+1) = *(pdata+2); data[3] = data [4] 間接運算子 * 優先權比算術運算子 + 或 - 高, 要記得加括號

  15. Ex4_10.cpp

  16. 動態記憶體配置 • 可自由使用的儲存空間-heap • 運算子new and delete • 又稱為free store

  17. new運算子 • 指標變數 = new 資料型態; • 指標變數 = new 資料型態[ 元素數 ]; • 第一種是配置單一指定資料型態的記憶體空間,傳回相同型態的指標 • 第二種是動態配置記憶體空間給陣列

  18. Example • 動態記憶體配置通常會利用指標 • 第二行敘述使用new運算子 • 由free store配置double型別變數的記憶體位址 • 記憶體位址將傳回並儲存在指標pvalue • 空間用完就無法配置 double* pvalue = NULL; pvalue = new double;

  19. 檢查配置是否成功 • 檢查動態配置是否成功 • exit()需要include標頭檔cstdlib if ( ! ( pvalue =new double) ) { cout<<endl <<“Out of memory.”; exit(1); }

  20. Example-配置與初始化 double* pvalue = NULL; pvalue = new double(999.0);

  21. delete 運算子 • 使用delete運算子釋放指定型態的記憶體空間 • delete 資料型態; • delete[ ] 資料型態; • 第一種是釋放配置單一元素型態的記憶體空間 • 第二種方式是釋放配置陣列的多個元素

  22. 參考值 Reference • 某變數的別名 • 可代替某變數名稱的名稱 • 參考值無法改變為另一變數的別名 • 參考值與指標的差別

  23. 參考值宣告及初始化 • 資料型態 & 變數名稱; long number; long& rnumber=number; rnumber += 10; • 參考值與指標的差異 long number; long* pnumber=&number; *pnumber += 10;

  24. Chapter 5 程式結構簡介

  25. C++的程式語法中可以把原本全部塞在主程式裡的程式碼細分到主程式之外,這些被細分出來的程式碼片段我們稱之為一個一個的「函式(function)」,而每個函式我們都可以針對它所專門負責執行的工作給予一個具有意義的函式名稱C++的程式語法中可以把原本全部塞在主程式裡的程式碼細分到主程式之外,這些被細分出來的程式碼片段我們稱之為一個一個的「函式(function)」,而每個函式我們都可以針對它所專門負責執行的工作給予一個具有意義的函式名稱

  26. 了解函式 • C++中函式名稱是全域的 • 函式名稱以數字、字母及底線組成 • 必須是字母或底線為開端 • 藉由引數(arguments)可將某些資料傳遞給函式 • 需配合函式定義時所定義的參數(parameter)

  27. Figure p172 引數 cout << AddInts( 2, 3); 函式定義 int AddInts( int i, int j) { return i + j; } 參數

  28. 函式宣告 • 傳回值的型態 • 函式名稱 • 函式參數 return_type function_name(parameters) { declare variables; function’s jobs; return(return_value); }

  29. 函式宣告 (2) • 合法資料型態都可以當作傳回值型態 • 不傳回任何值-傳回型態指定為void • 沒有參數時也是使用void • Ex: void MyFunc(void) • 或者void MyFunc()

More Related