鱼C论坛

 找回密码
 立即注册
查看: 2039|回复: 2

大神们救救我的代码

[复制链接]
发表于 2016-4-21 17:16:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
以下程序用意是查找用在不改变函数的情况下利用指针函数用同一个函数查找多个“关键字符串”,编译没问题,但运行出错,请帮忙,
代码如下:
  1. #include<stdio.h>
  2. #include<string.h>

  3. int NUM_ADS=7;
  4. char *ADS[]={
  5.                         "William:SBM GSOH likes sports,TV,dining",
  6.             "Matt: SWM NS likes art,movies,theater",
  7.                     "Luis:SLM ND likes books,theater,art",
  8.                         "Mike:DWM DS likes trucks,sports and bieber",
  9.                         "Peter: SAM likes chess,working out and art",
  10.                         "Josh:SJM likes sports,movies and theater",
  11.                         "Jed:DBM likes theater,books and dining",
  12.                         };
  13. int sports_no_bieber(char *s[])//筛选有“sports”不能有“bieber”的字符串
  14. {
  15.        
  16.         return (strstr(s,"sports")&&!strstr(s,"bieber"));
  17.          
  18. }
  19. int sports_or_workout(char *s[])//筛选有“sports”或workout的字符串
  20. {
  21.         return strstr(s,"sports")||strstr(s,"workout");
  22. }
  23. int ns_theater(char *s[])//筛选有“NS”和“theater”的字符串
  24. {
  25.         return strstr(s,"NS")&&strstr(s,"theater");
  26. }

  27. void find(int (*match)(char *s))//将搜索到的结果显示
  28. {
  29.         int i;
  30.         puts("搜索结果:");
  31.         puts("-------------------------");
  32.         for(i=0;i<NUM_ADS;i++)
  33.         {
  34.                 if(match(ADS[i]))
  35.                 {
  36.                         printf("%s\n",ADS[i]);
  37.                 }
  38.         }
  39.         puts("-------------------------");
  40. }

  41. void main()
  42. {
  43.         int (*no)(char);
  44.         int (*or)(char);
  45.         int (*ns)(char);
  46.        
  47.         no=sports_no_bieber;
  48.         or=sports_or_workout;
  49.         ns=ns_theater;
  50.        
  51.         find((no)(ADS));
  52.         find((or)(ADS));
  53.         find((ns)(ADS));

  54. }
复制代码

有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)
QQ截图20160421162620.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-4-25 11:56:52 | 显示全部楼层

卤煮,不开玩笑地说:我也被你迷惑了!

你的find需要的函数是函数指针,但是你给的是啥?
正确:find(no);
错误:就像你写的。

原因:你find(no(ADS))调用,其中参数no(ADS)已经执行了···不要闹,执行了返回一个int,所以你的find就不认识了。

并且,你的三个查找函数的参数类型也是醉了,不要粗心啊!!!(第一眼看不出错误,自己掌嘴)

此致,敬礼!
有问题,再讨论。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-5-7 10:56:05 | 显示全部楼层
问题已解决,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-22 11:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表