#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,movie theatre",
"Jed:DBM likes theater,books and dining"
};
int sports_no_bieber(char *s)
{
return strstr(s,"sports")&&!strstr(s,"bieber");
}
int sports_or_workout(char *s)
{
return strstr(s,"sports")||strstr(s,"working out");
}
int ns_theater(char *s)
{
return strstr(s,"NS")&&strstr(s,"theater");
}
int arts_theater_or_dining(char *s
{
return strstr(s,"arts")||strstr(s,"theater")||strstr(s,"dining");
}
创建指向上面定义的函数的指针
void find(int(*match)(char*))
{
int i;
puts("Search Results:");
puts("------------------------------------");
for(i=0;i<NUM_ADS;i++)
{
if(match(ADS[i]))
{
printf("%s\n",ADS[i]);
}
}
puts("----------------------------------");
}
并将这些指针作为参数传递给这些函数,我得到了错误分段错误
int main()
{
find(sports_no_bieber);
find(sports_or_workout);
find(ns_theater);
find(arts_theater_or_dining);
return 0;
}
我创建函数指针并将它们传递给另一个函数
我在valgrind查过了
Valgrind输出
可执行输出
我正在使用ubuntu 17.04