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 }

 



    

posted @ 2017-06-08 06:38  叕叒双又  阅读(692)  评论(0编辑  收藏  举报