34深入理解C指针之---通过字符串传递函数
一、通过字符串传递函数
1、定义:可以使用函数名(字符串)调用函数,也可以使用函数指针调用函数,将两者结合
2、特征:
1)、在函数声明时使用函数指针
2)、调用函数时使用函数名称(字符串)
3)、可以让函数的调用更加灵活方便
3、应用代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <ctype.h> 5 6 char *stringToLower(const char *string){ 7 char *tmp = (char *)malloc(strlen(string) + 1); 8 char *start = tmp; 9 while(*string != 0){ 10 *tmp++ = tolower(*string++); 11 } 12 *tmp = 0; 13 14 return start; 15 } 16 17 int compare(const char *s1, const char *s2){ 18 return strcmp(s1, s2); 19 } 20 21 int compareIgnoreCase(const char *s1, const char *s2){ 22 char *t1 = stringToLower(s1); 23 char *t2 = stringToLower(s2); 24 int result = strcmp(t1, t2); 25 26 free(t1); 27 free(t2); 28 29 return result; 30 } 31 32 typedef int (fptrOperation)(const char *, const char *); 33 34 void sort(char *array[], int size, fptrOperation operation){ 35 int swap = 1; 36 while(swap){ 37 swap = 0; 38 for(int i = 0; i < size - 1; i++){ 39 if(operation(array[i], array[i + 1]) > 0){ 40 swap = 1; 41 char *tmp = array[i]; 42 array[i] = array[i + 1]; 43 array[i + 1] = tmp; 44 } 45 } 46 } 47 } 48 49 void displayArray(char *names[], int size){ 50 for(int i = 0; i < size; i++){ 51 printf("%s\t", names[i]); 52 } 53 54 printf("\n"); 55 } 56 57 int main(int argc, char **argv) 58 { 59 char *names[] = {"Bob", "Ted", "Carol", "Alice", "dlice",}; 60 int size = sizeof(names); 61 62 printf("Using compare sort: "); 63 sort(names, 5, compare); 64 displayArray(names, 5); 65 66 printf("Using compareIgnore sort: "); 67 sort(names, 5, compareIgnoreCase); 68 displayArray(names, 5); 69 70 return 0; 71 }
代码说明:
1)、stringToLower函数主要是将字符串转换为小写
2)、compare函数实现字符串的比较,区分大小写
3)、compareIgnoreCase函数实现字符串的比较,不区分大小写
4)、sort函数实现字符串的排序
5)、displayArray函数主要用来显示字符串
6)、main函数主要用来测试实现的功能是否正常
7)、第32行代码定义函数指针fptrOperation
8)、第63行,调用字符串和自行指定比较函数(区分大小写比较),实现字符串的比较
9)、第67行,调用字符串和自行指定比较函数(不区分大小写比较),实现字符串的比较
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <ctype.h> 5 6 char *stringToLower(const char *string){ 7 char *tmp = (char *)malloc(strlen(string) + 1); 8 char *start = tmp; 9 while(*string != 0){ 10 *tmp++ = tolower(*string++); 11 } 12 *tmp = 0; 13 14 return start; 15 } 16 17 int compare(const char *s1, const char *s2){ 18 return strcmp(s1, s2); 19 } 20 21 int compareIgnoreCase(const char *s1, const char *s2 ){ 22 char *t1 = stringToLower(s1); 23 char *t2 = stringToLower(s2); 24 25 int result = strcmp(t1, t2); 26 free(t1); 27 free(t2); 28 29 return result; 30 } 31 32 typedef int (fptrOperation)(const char*, const char *); 33 34 void sort(char *array[], int size, fptrOperation operation){ 35 int swap = 1; 36 while(swap){ 37 swap = 0; 38 for(int i = 0; i < size; i++){ 39 if(operation(array[i], array[i + 1]) > 0){ 40 char *tmp = array[i]; 41 array[i] = array[i + 1]; 42 array[i + 1] = tmp; 43 } 44 } 45 } 46 } 47 48 void displayArr(char *names[], int size){ 49 for(int i = 0; i < size; i++){ 50 printf("%s\t", names[i]); 51 } 52 53 printf("\n"); 54 } 55 56 int main(int argc, char **argv) 57 { 58 char *names[] = {"Bob", "Ted", "Carol", "alice", "Dlice",}; 59 60 sort(names, 5, compare); 61 displayArr(names, 5); 62 63 sort(names, 5, compareIgnoreCase); 64 displayArr(names, 5); 65 66 return 0; 67 }
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具