首页 > 代码库 > 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之函数指针
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。