首页 > 代码库 > C之函数指针

C之函数指针

这方面的技能,在观察系统调用时,是很需要的。

但我不是很熟悉,另外,要再会函数指针数组的话,那就更强啦~~

按《HEAD FIRST C》作了个样例:

cat find.c #include <stdio.h>#include <string.h>int NUM_ADS = 7;char *ADS[] = {  "William: SBM GSOH like sports, TV, dining",  "Matt: SWM NS likes art, movie, theater",  "Peter: SHN like out ,art",  "DWM: DS likes truncks, sports and bieber",  "Luis: SLM ND likes books, theater, art",  "Josh: SJM likes sports, movies and theater",  "Mike: DWM DS likes trucks, sports and bieber"  };void find_raw() {  int i;  puts("Raw search results:");  puts("=================");  for (i = 0; i < NUM_ADS; i++) {        if (strstr(ADS[i], "sports" )          && !strstr(ADS[i], "bieber")) {          printf("%s\n", ADS[i]);        }  }  puts("================");}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, "workout");}int theater_or_dining(char *s){  return 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]);    }    if (!match(ADS[i])) {      printf("$$$$$$$$$$$$$\n");    }  }  puts("*************************");}int main() {  find_raw();  find(sports_or_workout);  find(&theater_or_dining);  return 0;}

C之函数指针