-3

我编写了一个使用函数指针来比较字符串的代码。但是,它向我显示错误,我不知道如何纠正它们。这是代码:

#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 函数……但这没有任何区别。错误究竟是什么意思?

4

2 回答 2

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_ADS并且ADS是正确的,使用指针终止NUM_ADS数组NULL并将其用作循环终止条件(和 discard 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而不是char*因为没有函数修改内容或重新分配指针。

于 2013-05-08T12:37:34.663 回答
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 )
于 2013-05-08T12:45:17.160 回答