Presentation is loading. Please wait.

Presentation is loading. Please wait.

實習八 函式指標.

Similar presentations


Presentation on theme: "實習八 函式指標."— Presentation transcript:

1 實習八 函式指標

2 指向函式的指標 函式在記憶體中都佔有一個位址, 指標可指向該位址。

3 宣告方式 資料型別(*函式指標名稱)(); void main() { int func(); int (*fp)(); fp=func;
printf("func's return value:%d\n", func()); printf("Using functional pointer:%d", (*fp)()); } func(){ return 123; }

4 宣告例子 Int (*test_ptr)(int, int)

5 函式指標陣列 int (*compare[10])(int, int); 上面這個宣告產生具有10個元素的陣列,可以儲存10個函式的位址,而函式形態為回傳int,引入參數為兩個int變數。

6 函式指標陣列範例 int f0(),f1(),f2(),f3(),f4(); static int (*dispatch[])()= {
}; dispatch[0] f0() 指向f0() dispatch[1] f1() 指向f1()

7 呼叫方式 Int fun0(int, int); int (*fun_ptr)(int, int); fun_ptr = fun0;

8 實習八 撰寫一程式,讓使用者可以選擇將要處理的動作,使主程式可以與使用者對談。 程式須包含下列幾個函式
1. strcat() 功能為傳入兩字串指標後回傳合併後的字串 2. strswap() 功能為交換輸入的兩字串 3. strplace() 先輸入一字串,接著要求使用者輸入想更改的第幾個字母,再將字母更改成使用者指定的字 4. bye() 功能為印出thanks bye!並離開程式

9 注意事項 在此作業中,禁止使用直接對函式呼叫,也 是strcat(a, b)此種呼叫方式,皆需使用函 式指標做函式呼叫的動作。

10 加分題 使用函式指標陣列完成作業要求 建立函式指標陣列,儲存各個函式,在依使用者的選擇回傳結果。 並且
撰寫函式 strcmp() ,在執行此一函式時會要求使用者輸入將被搜尋的字串,接著輸入要找尋的字串內容,經過搜尋後印出所有出現此字串頭的位址. Ex.被搜尋字串:abcmpdddcmp123 關鍵字:cmp 需印出: abcmpdddcmp123兩個cmp的起始位址


Download ppt "實習八 函式指標."

Similar presentations


Ads by Google