我编写了一个使用函数指针来比较字符串的代码。但是,它向我显示错误,我不知道如何纠正它们。这是代码:
#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
我什至尝试将 find 函数变成一个 int 函数……但这没有任何区别。错误究竟是什么意思?