在函数指针代码中显示错误

时间:2013-05-08 12:35:16

标签: c function pointers

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

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

2 个答案:

答案 0 :(得分: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_ADSADS正确使用NUM_ADS指针终止NULL数组并将其用作循环终止条件(并丢弃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*,因为没有任何函数可以修改内容或重新分配指针。

答案 1 :(得分: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 )