马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
以下程序用意是查找用在不改变函数的情况下利用指针函数用同一个函数查找多个“关键字符串”,编译没问题,但运行出错,请帮忙,
代码如下:#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[i]))
{
printf("%s\n",ADS[i]);
}
}
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 *[7]'
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 *[7]'
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 *[7]'
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)
|