1 / 16

第八章 指標 (Pointers)

第八章 指標 (Pointers). 8.1 何謂指標. 指標簡單的說就是記憶體的位置,大多數的高階語言都將記憶體的管理視為系統內部的工作。不允許程式設計者輕易接觸,怕引起破壞系統而當機。但 C 語言不同,它提供指標的機制,讓程式設計人員充分掌握系統資源,但水可載舟也可覆舟,應用指標仍以小心為宜。 個人電腦的記憶體通常以位元組 (byte) 為基本單位,每一個位元組都有一個編號,這個編號就稱為位址,就像門牌編號一樣,有門牌編號郵差才可以將信件送達,同理有記憶體位址,程式才可將資料送達。. int i=7; /* 宣告變數 i 為整數變數, 初值為 7 */.

etoile
Download Presentation

第八章 指標 (Pointers)

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. 第八章 指標(Pointers) 8.1 何謂指標 • 指標簡單的說就是記憶體的位置,大多數的高階語言都將記憶體的管理視為系統內部的工作。不允許程式設計者輕易接觸,怕引起破壞系統而當機。但C語言不同,它提供指標的機制,讓程式設計人員充分掌握系統資源,但水可載舟也可覆舟,應用指標仍以小心為宜。 • 個人電腦的記憶體通常以位元組(byte)為基本單位,每一個位元組都有一個編號,這個編號就稱為位址,就像門牌編號一樣,有門牌編號郵差才可以將信件送達,同理有記憶體位址,程式才可將資料送達。

  2. int i=7; /*宣告變數i為整數變數, 初值為7 */ 1025 1024 00 1025 1024 07 i 0000 0000 0000 0111 Fig 8.2 內含值7以十六進位表示 Fig 8.1 內含值7以二進位表示 int i=7; /*宣告變數i為整數變數, 初值為7 */ 1025 1024 位址FFD6 內含值 7 7 i 變數名稱 i Fig 8.3 內含值7以十進位表示 Fig 8.4 變數i位址FFD6以十六進位表示,內含以十進位表示

  3. 8.2 指標變數 • 一般的變數其內含值為數值的,如整數變數其內含值為整數值,浮點數變數其內含值為浮點數值,其宣告為: int i=7; /*宣告變數i為整數變數,初值為7 */ float f=365.25; /*宣告變數f為浮點數變數,初值為365.25 */ 若有一個變數其內含值為位址,這種變數稱為指標變數值 (pointer variable),其宣告為: int i=7; /*宣告變數i為整數變數,初值為7 */ float f=365.25; /*宣告變數f為浮點數變數,初值為365.25 */ int *ip=&I; /*宣告變數ip為指標變數,初值為i的位址 */ 宣告ip為指標變數,初值為i的位址,如Fig 8.6所示。

  4. float f=365.25; float f2=12.5; float *fp=&f; /*將f的位址指定 給指標變數fp*/ … fp=&fp2; /*將f2的位址指定 給指標變數fp*/ FFD6 FFD6 7 ip i Fig 8.6 指標變數ip的初值 為變數I的位址 FFD6 FFD6 FFD6 365.25 FFDA 365.25 fp f fp f FFDA FFDA 12.5 12.5 f2 f2 Fig 8.7 將變數f的位址指定 給指標變數fp Fig 8.8 將變數f2的位址指定 給指標變數fp

  5. 8.3 指標應用

  6. 8.4 指標與陣列 位址 FFD4 FFD6 FFD8 FFDA FFDC a 1 3 5 7 9 內含值 註標 0 1 2 3 4 元素變數 a[0] a[1] a[2] a[3] a[4] *a *(a+1) *(a+2) *(a+3) *(a+4) 指標變數 Fig 8.16 陣列變數名稱a,即為該陣列 第0個元素a[0]的位址

  7. 8.5 陣列引數及指標參數 • 程式經過編譯過後所有的變數都轉換成記憶體的一個位址,陣列也是一樣的,陣列的名稱實際上就是起始元素的位址,例如 : int a[5]={1,3,5,7,9}; a陣列的位址就是a[0]元素的位址,在函式呼叫時你可以用傳值呼叫(call by value),也可以用傳址呼叫(call by adress),如此可以將a陣列的位址傳給函式,函式就取得a陣列的所有元素了,不須將龐大的陣列元素一個一個傳給函式,浪費記憶體空間,也浪費時間。 例題0805使用函式使用函式呼叫將引數5傳值給printArray()函式的參數n,將陣列a的位址給參數指標變數b。 printArray(5,a); 也就是說b到a陣列,如Fig8.17所示。 a FFD5 FFD6 FFD7 FFD8 FFD9 FFD4 1 3 5 7 9 b FFD4 0 1 2 3 4 Fig 8.17 將引數5傳值給參數n,a位址傳址給參數b

  8. Chap 12 Input and Output 12.1 BASIC CONCEPTS • Until this point, our computer programs have used the scanf() function for program input from the keyboard and printf() for program output to the computer screen. 12.4 FILE INPUT/OUTPUT • The five operations a C programmer can conduct on a file are : • Open it for reading, writing, or appending. • Close it. • Read from it at the current position. • Write to it at the current position. • Reposition the place where the next file I/O operation will take place. FILE *FpStream;

  9. 12.4.1 Opening and Closing Files • FILE *fopen(const char *cpFilename,const char *cpMode); int fclose(FILE *FpStream); • Table 12.5 Modes of I/O

  10. Example : To see how these concepts work in practice, the following fragment of code use these functions to open a file for writing.

  11. 12.4 Functions fprintf() and fscanf() • The functions fprintf() and fscanf() are the file counterparts of printf() and scanf(). The function prototypes are int fprintf(FILE *Fpstream,const char *kcpFormat [, argumemt, …]); int fscanf(FILE *Fpstream,const char *kcpFormat [, address, …]);

More Related