[問題] array of function pointer 宣告(已解決)

看板C_and_CPP作者 (策)時間12年前 (2012/03/07 23:21), 編輯推噓0(0018)
留言18則, 6人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) GCC 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): http://en.cppreference.com/w/cpp/string/byte/strstr strstr(char *, char *) 回傳 char * strcasestr(char *, char *) 回傳 char * 所以我 array of function pointer 這樣宣告: char *(*findstr[2]) (char *, char *) findstr[0] = strstr; findstr[1] = strcasestr; 請問這樣哪裡錯了?? 謝謝 錯誤結果(Wrong Output): compile 結果 59: warning: assignment from incompatible pointer type 60: warning: assignment from incompatible pointer type 65: warning: assignment makes pointer from integer without a cast 程式碼(Code):(請善用置底文網頁, 記得排版) 完整版 http://codepad.org/caCLKPn2 前略 56 char *ptr = strBuf; 57 char *(*findstr[2])(char *, char *); 58 59 findstr[0] = strstr; 60 findstr[1] = strcasestr; 61 63 while (1) { 64 65 if ((ptr = findstr[flag_i]) != NULL) { // 可能 flag_i == 0 或 1 後略 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.222.191

03/07 23:28, , 1F
typedef char *(*FINDSTR_PTR)(char*, char*);
03/07 23:28, 1F

03/07 23:28, , 2F
FINDSTR_PTR findstr[2];
03/07 23:28, 2F

03/07 23:28, , 3F
我會寫成這樣,對我來說比較容易理解...XD
03/07 23:28, 3F

03/07 23:30, , 4F
話說,你不能忽視 const 這個字
03/07 23:30, 4F

03/08 00:55, , 5F
其實... 我看不太懂什麼const呢 ~"~
03/08 00:55, 5F

03/08 00:56, , 6F
話說.以前雖看過但我不能理解這裡用typedef比較容易理解
03/08 00:56, 6F

03/08 00:58, , 7F
標準函式是 char* strstr(char*, const char*) 或
03/08 00:58, 7F

03/08 00:59, , 8F
const char* strstr(const char*, const char*)
03/08 00:59, 8F

03/08 01:00, , 9F
所以char* (char*,char*) 會incompatible
03/08 01:00, 9F

03/08 01:07, , 10F
了解 但是 char *(*findstr[2])(char *, const char *);
03/08 01:07, 10F

03/08 01:07, , 11F
我這樣宣告 結果還是錯呢....~"~
03/08 01:07, 11F

03/08 01:08, , 12F
compile 結果還是一樣那三行
03/08 01:08, 12F

03/08 01:25, , 13F
const char *(*findstr[2])(const char *, const char *);
03/08 01:25, 13F

03/08 01:26, , 14F
用Borland C++ Compiler 5.5測試可以正常編譯
03/08 01:26, 14F

03/08 01:26, , 15F
如j大講的"不能忽視 const"
03/08 01:26, 15F
http://codepad.org/caCLKPn2 compile 結果 59: warning: assignment from incompatible pointer type 60: warning: assignment from incompatible pointer type 不過執行起來是沒問題的.... 功能: 用A字串取代B字串 在命令列上, 加上 -i 不分大小寫 (使用 strcasestr) 不加 -i 則是會分 (使用 strstr) 這是作業,不過沒有用到 function pointer 的版本我都交出去了 (上完課剛學到 function pointer 想練習一下...) 因為像是把這行 if ((ptr = findstr[i]) != NULL) { 寫成 if (flag_i == 0 && (ptr = strstr(ptr, pattern_1)) != NULL || flag_i == 1 && (ptr = strcasestr(ptr, pattern_1)) != NULL) { 或是拆成兩塊都跑得動,compile 也不出錯。 謝謝解惑! ※ 編輯: manoeuvre 來自: 140.123.222.191 (03/08 08:21) ※ 編輯: manoeuvre 來自: 140.123.222.191 (03/08 08:21)

03/08 08:23, , 16F
你用的編譯環境是?
03/08 08:23, 16F
FreeBSD+GCC

03/08 09:10, , 17F
應該要寫 if ((ptr = findstr[i]) != NULL) 吧?
03/08 09:10, 17F
這行後來有看到,也改過了 ※ 編輯: manoeuvre 來自: 140.123.22.28 (03/08 14:54) ※ 編輯: manoeuvre 來自: 140.123.22.28 (03/08 15:13) 結果: ---- TRSTR(3) FreeBSD Library Functions Manual STRSTR(3) NAME strstr, strcasestr, strnstr -- locate a substring in a string LIBRARY Standard C Library (libc, -lc) SYNOPSIS #include <string.h> char * strstr(const char *big, const char *little); char * strcasestr(const char *big, const char *little); ---- 後來改成 57 char *(*findstr[2])(const char *, const char *); compile 不再出現 warning了 ※ 編輯: manoeuvre 來自: 140.123.222.191 (03/08 22:32)

03/09 00:19, , 18F
You got it finally!
03/09 00:19, 18F
文章代碼(AID): #1FLtqGyh (C_and_CPP)