2013-05-08 33 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)