2013-05-08 36 views
-2

我寫了一個使用函數指針來比較字符串的代碼。但是,它顯示了我的錯誤,我不知道如何糾正它們。下面是代碼:在函數指針代碼中顯示錯誤

#include<stdio.h> 
#include<string.h> 
void sports_no_bieber(char *); 
void science_sports(char *); 
void theater_no_guys(char *); 
int find(int(*match)(char*)); 
int NUM_ADS=4; 
char *ADS[]={ 
       "Sarah:girls, sports, science", 
       "William: sports, TV, dining", 
       "Matt: art, movies, theater", 
       "Luis: books, theater, guys", 
       "Josh: sports, movies, theater" 
      }; 
int main() 
{ 
printf("Bachelorette Amanda needs your help! He wants someone who likes sports but not bieber.\n"); 
find(sports_no_bieber); 
printf("Bachelorette Susan needs your help! She wants someone who likes science and sports. (And girls).\n"); 
find(science_sports); 
printf("Bachelorette Emily needs your help! She wants someone who likes theater but not guys.\n"); 
find(theater_no_guys); 
return 0; 
} 



int find(int(*match)(char*)) 
{ 
     int i; 
     puts("Search results\n"); 
puts("--------------------"); 
for(i=0;i<NUM_ADS;i++) 
{ 
    if(match(ADS[i])) 
     printf("%s\n",ADS[i]; 
} 
puts("--------------------"); 
return i; 
} 

int sports_no_bieber(char * s) 
{ 
return (strstr(s, "sports")) && (!strstr (s,"bieber")); 
} 

int science_sports(char * s) 
{ 
return (strstr(s, "science")) && (strstr (s,"sports")); 
} 

int theater_no_guys(char * s) 
{ 
return (strstr(s, "theater"))&&(!strstr(s,"guys")); 
} 

,並顯示錯誤是

E:\ComputerPrograming\FunctionPointer.c: In function `int main()': 
E:\ComputerPrograming\FunctionPointer.c:18: passing `void (*)(char *)' as argument 1 of `find(int (*)(char *))' 
E:\ComputerPrograming\FunctionPointer.c:20: passing `void (*)(char *)' as argument 1 of `find(int (*)(char *))' 
E:\ComputerPrograming\FunctionPointer.c:22: passing `void (*)(char *)' as argument 1 of `find(int (*)(char *))' 
E:\ComputerPrograming\FunctionPointer.c: In function `int find(int (*)(char *))': 
E:\ComputerPrograming\FunctionPointer.c:36: parse error before `;' 
E:\ComputerPrograming\FunctionPointer.c:40: confused by earlier errors, bailing out 

我甚至試圖使查找功能爲一個int功能......但沒有任何區別。錯誤究竟意味着什麼?

+5

你的函數原型不匹配你的函數定義... – Mat 2013-05-08 12:36:55

回答

4

這些函數聲明:

void sports_no_bieber(char *); 
void science_sports(char *); 
void theater_no_guys(char *); 

不要find()或它們的定義所需的函數指針的簽名相匹配。更改爲:

int sports_no_bieber(char *); 
int science_sports(char *); 
int theater_no_guys(char *); 

注意NUM_ADS不等於ADS數組中元素的數目:它是一個更小。爲了避免必須確保NUM_ADSADSNULL指針被正確終止NUM_ADS陣列,並用它作爲循環終止條件(並丟棄NUM_ADS):

const char *ADS[] = 
{ 
    "Sarah:girls, sports, science", 
    "William: sports, TV, dining", 
    "Matt: art, movies, theater", 
    "Luis: books, theater, guys", 
    "Josh: sports, movies, theater", 
    NULL 
}; 

for(int i=0; ADS[i]; i++) 
{ 

推薦使所有功能的參數類型爲const char* const代替因爲沒有任何功能修改內容或重新分配指針。

+0

啊。感謝您指出了這一點。這對我來說相當愚蠢。 – 2013-05-08 12:45:46

1

您有兩種類型的錯誤,首先是原型和函數本身之間的不匹配。

void sports_no_bieber(char *); 
^  ^  ^
|   |   | 
these much mach  the types here must 
    exactly    match 
|   |   | 
v   v   v 
int sports_no_bieber(char * s) 

因此,您需要名稱和返回類型相同,就像使用參數類型一樣。在您的情況下,退貨類型不匹配sports_no_bieber(),science_sports()theater_no_guys()

避免這個問題的一種方法是將功能定義移到使用點上方,這樣就不需要原型,並消除了誤弄它們的機會......當然,您也可以複製和粘貼以避免這樣的愚蠢錯誤。

您有其他錯誤是在你的find()功能,你錯過了一個括號:

printf("%s\n",ADS[i]; // <-- missed the close)