大神们救救我的代码
以下程序用意是查找用在不改变函数的情况下利用指针函数用同一个函数查找多个“关键字符串”,编译没问题,但运行出错,请帮忙,代码如下:#include<stdio.h>
#include<string.h>
int NUM_ADS=7;
char *ADS[]={
"William:SBM GSOH likes sports,TV,dining",
"Matt: SWM NS likes art,movies,theater",
"Luis:SLM ND likes books,theater,art",
"Mike:DWM DS likes trucks,sports and bieber",
"Peter: SAM likes chess,working out and art",
"Josh:SJM likes sports,movies and theater",
"Jed:DBM likes theater,books and dining",
};
int sports_no_bieber(char *s[])//筛选有“sports”不能有“bieber”的字符串
{
return (strstr(s,"sports")&&!strstr(s,"bieber"));
}
int sports_or_workout(char *s[])//筛选有“sports”或workout的字符串
{
return strstr(s,"sports")||strstr(s,"workout");
}
int ns_theater(char *s[])//筛选有“NS”和“theater”的字符串
{
return strstr(s,"NS")&&strstr(s,"theater");
}
void find(int (*match)(char *s))//将搜索到的结果显示
{
int i;
puts("搜索结果:");
puts("-------------------------");
for(i=0;i<NUM_ADS;i++)
{
if(match(ADS))
{
printf("%s\n",ADS);
}
}
puts("-------------------------");
}
void main()
{
int (*no)(char);
int (*or)(char);
int (*ns)(char);
no=sports_no_bieber;
or=sports_or_workout;
ns=ns_theater;
find((no)(ADS));
find((or)(ADS));
find((ns)(ADS));
}
有N个“waring”:
D:\小程序\查找程序(指针函数)\fine.c(17) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char ** '
D:\小程序\查找程序(指针函数)\fine.c(17) : warning C4024: 'strstr' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(17) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char ** '
D:\小程序\查找程序(指针函数)\fine.c(17) : warning C4024: 'strstr' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(22) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char ** '
D:\小程序\查找程序(指针函数)\fine.c(22) : warning C4024: 'strstr' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(22) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char ** '
D:\小程序\查找程序(指针函数)\fine.c(22) : warning C4024: 'strstr' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(26) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char ** '
D:\小程序\查找程序(指针函数)\fine.c(26) : warning C4024: 'strstr' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(26) : warning C4047: 'function' : 'const char *' differs in levels of indirection from 'char ** '
D:\小程序\查找程序(指针函数)\fine.c(26) : warning C4024: 'strstr' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(50) : warning C4028: formal parameter 1 different from declaration
D:\小程序\查找程序(指针函数)\fine.c(51) : warning C4028: formal parameter 1 different from declaration
D:\小程序\查找程序(指针函数)\fine.c(52) : warning C4028: formal parameter 1 different from declaration
D:\小程序\查找程序(指针函数)\fine.c(54) : warning C4047: 'function' : 'char ' differs in levels of indirection from 'char *'
D:\小程序\查找程序(指针函数)\fine.c(54) : warning C4024: 'no' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(54) : warning C4047: 'function' : 'int (__cdecl *)(char *)' differs in levels of indirection from 'int '
D:\小程序\查找程序(指针函数)\fine.c(54) : warning C4024: 'find' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(55) : warning C4047: 'function' : 'char ' differs in levels of indirection from 'char *'
D:\小程序\查找程序(指针函数)\fine.c(55) : warning C4024: 'or' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(55) : warning C4047: 'function' : 'int (__cdecl *)(char *)' differs in levels of indirection from 'int '
D:\小程序\查找程序(指针函数)\fine.c(55) : warning C4024: 'find' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(56) : warning C4047: 'function' : 'char ' differs in levels of indirection from 'char *'
D:\小程序\查找程序(指针函数)\fine.c(56) : warning C4024: 'ns' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(56) : warning C4047: 'function' : 'int (__cdecl *)(char *)' differs in levels of indirection from 'int '
D:\小程序\查找程序(指针函数)\fine.c(56) : warning C4024: 'find' : different types for formal and actual parameter 1
D:\小程序\查找程序(指针函数)\fine.c(54) : warning C4761: integral size mismatch in argument; conversion supplied
D:\小程序\查找程序(指针函数)\fine.c(55) : warning C4761: integral size mismatch in argument; conversion supplied
D:\小程序\查找程序(指针函数)\fine.c(56) : warning C4761: integral size mismatch in argument; conversion supplied
fine.obj - 0 error(s), 30 warning(s)
卤煮,不开玩笑地说:我也被你迷惑了!
你的find需要的函数是函数指针,但是你给的是啥?
正确:find(no);
错误:就像你写的。
原因:你find(no(ADS))调用,其中参数no(ADS)已经执行了···不要闹,执行了返回一个int,所以你的find就不认识了。
并且,你的三个查找函数的参数类型也是醉了,不要粗心啊!!!(第一眼看不出错误,自己掌嘴)
此致,敬礼!
有问题,再讨论。 问题已解决,谢谢
页:
[1]