[問題] array of function pointer 宣告(已解決)
開發平台(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
03/07 23:28, 1F
→
03/07 23:28, , 2F
03/07 23:28, 2F
→
03/07 23:28, , 3F
03/07 23:28, 3F
→
03/07 23:30, , 4F
03/07 23:30, 4F
→
03/08 00:55, , 5F
03/08 00:55, 5F
→
03/08 00:56, , 6F
03/08 00:56, 6F
→
03/08 00:58, , 7F
03/08 00:58, 7F
→
03/08 00:59, , 8F
03/08 00:59, 8F
→
03/08 01:00, , 9F
03/08 01:00, 9F
→
03/08 01:07, , 10F
03/08 01:07, 10F
→
03/08 01:07, , 11F
03/08 01:07, 11F
→
03/08 01:08, , 12F
03/08 01:08, 12F
→
03/08 01:25, , 13F
03/08 01:25, 13F
→
03/08 01:26, , 14F
03/08 01:26, 14F
→
03/08 01:26, , 15F
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
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
03/09 00:19, 18F