首页 > 代码库 > 8.4 数组指针 函数指针

8.4 数组指针 函数指针

数组指针的含义:
一个指针指向一个数组 ,这个指针+1就会加一个数组的长度。
  1. #include <stdio.h>
  2. void show( char(*s)[10], int n)
  3. {
  4. while(n--)
  5. printf("%s\n",s++);
  6. }
  7. int main()
  8. {
  9. char s[3][10] = {"hello","liuwei","xuanyuan"};
  10. show(s,3);
  11. return 0;
  12. }


指针数组与数组指针的区别:

char *s[] = { "hello", "world", "liuwei"};          //指针数组

内存地址如下:

S这里存储的是3个地址,即这个数组存了三个指针

然后该地址位置存储了常量字符串



char s[3][8] = { "hello", "world", "liuwei" };   //数组指针   s+1就相当于+8(列)个字节     二位数组等价于数组指针

内存地址如下:




函数指针的含义:
保存的是函数的地址
函数名的本质,表示此函数第一条指令的地址,可以说是函数的入口地址
  1. #include <stdio.h>
  2. void show(char * s)
  3. {
  4. printf("%s\n",s);
  5. return;
  6. }
  7. int main()
  8. {
  9. char str[] = "hello world";
  10. show(str);
  11. void(*p)(char *); //这两行代码可以直接写成下面的
  12. p = show; //void(*p)(char *) = show;
  13. p(str);
  14. return 0;
  15. }



请定义出以下函数的函数指针
1. int mystrlen(char *str)
2. char *mystrcpy(char *dest, char *src)
3. int mystrcmp(char *s1, char *s2)
4. int word_sort(char *s[], int n)
5. char *max_str(char *s[], int n, char **max)

1. int (*p)(char *);
2. char * (*p)(char *, char *);
3. int (*p)(char *, char *)
4. int (*p)(char **, int)
5. char * (*p)(char **, int, char **)

int (*p[10])(char *);函数指针数组



函数指针应用 :
  1. #include <stdio.h>
  2. struct STU
  3. {
  4. int id;
  5. char name[20];
  6. char sex;
  7. void(*p)(void);
  8. };
  9. void st1(void)
  10. {
  11. printf("I am st1\n");
  12. return;
  13. }
  14. void st2(void)
  15. {
  16. printf("I am st2\n");
  17. return;
  18. }
  19. int main(void)
  20. {
  21. struct STU a = { 10, "liuwei", ‘M‘, st1 };
  22. struct STU b = { 20, "xuanyuan", ‘W‘, st2 };
  23. printf("%d %s %c\n", a.id, a.name, a.sex);
  24. a.p();
  25. printf("%d %s %c\n", b.id, b.name, b.sex);
  26. b.p();
  27. return 0;
  28. }


函数指针就是c++的方法实现(php同理):

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct STU
  4. {
  5. int id;
  6. char name[20];
  7. char sex;
  8. void(*show)(pS);
  9. }S, *pS;
  10. void show(pS this)
  11. {
  12. printf("%d %s %c\n", this->id, this->name, this->sex);
  13. }
  14. int main(void)
  15. {
  16. S a = { 10, "liuwei", ‘M‘, show };
  17. a.show(&a);
  18. return 0;
  19. }



?函数指针数组实用案例:

通过输入不同的数字,对struct进行排序
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define N 3
  6. typedef struct STU
  7. {
  8. int id;
  9. char name[20];
  10. char sex;
  11. int score;
  12. }S, *pS;
  13. void sort_id(pS a, int n)
  14. {
  15. int i, j, min;
  16. S temp;
  17. for (i = 0; i < n; ++i)
  18. {
  19. min = i;
  20. for (j = i + 1; j < n; ++j)
  21. if (a[min].id > a[j].id)
  22. min = j;
  23. temp = a[min];
  24. a[min] = a[i];
  25. a[i] = temp;
  26. }
  27. return;
  28. }
  29. void sort_name(pS a, int n)
  30. {
  31. int i, j, min;
  32. S temp;
  33. for (i = 0; i < n; ++i)
  34. {
  35. min = i;
  36. for (j = i + 1; j < n; ++j)
  37. if (strcmp(a[min].name, a[j].name) > 0)
  38. min = j;
  39. temp = a[min];
  40. a[min] = a[i];
  41. a[i] = temp;
  42. }
  43. return;
  44. }
  45. void sort_score(pS a, int n)
  46. {
  47. int i, j, min;
  48. S temp;
  49. for (i = 0; i < n; ++i)
  50. {
  51. min = i;
  52. for (j = i + 1; j < n; ++j)
  53. if (a[min].score > a[j].score)
  54. min = j;
  55. temp = a[min];
  56. a[min] = a[i];
  57. a[i] = temp;
  58. }
  59. return;
  60. }
  61. void init_stu(pS a, int n)
  62. {
  63. a[0].id = 50;
  64. strcpy(a[0].name, "liuwei");
  65. a[0].score = 78;
  66. a[0].sex = ‘M‘;
  67. a[1].id = 60;
  68. strcpy(a[1].name, "xuanyu");
  69. a[1].score = 90;
  70. a[1].sex = ‘W‘;
  71. a[2].id = 10;
  72. strcpy(a[2].name, "hello");
  73. a[2].score = 63;
  74. a[2].sex = ‘M‘;
  75. }
  76. void show_stu(pS a, int n)
  77. {
  78. int i;
  79. for (i = 0; i < n; ++i)
  80. {
  81. printf("%d\t%s\t%c\t%d\n", a[i].id, a[i].name, a[i].sex, a[i].score);
  82. }
  83. }
  84. int main(void)
  85. {
  86. int n;
  87. struct STU a[N];
  88. init_stu(a, N);
  89. show_stu(a, N);
  90. printf("请输入以下数字排序:\n0---sort_id\n1---sort_name\n2---sort_score\n");
  91. void(*fun[])(pS, int) = { sort_id, sort_name, sort_score }; //这是个函数指针数组
  92. scanf("%d", &n);
  93. while (n > sizeof(fun) / sizeof(fun[0]) - 1)
  94. {
  95. printf("请输入以下数字排序:\n0---sort_id\n1---sort_name\n2---sort_score\n");
  96. scanf("%d", &n);
  97. }
  98. fun[n](a, N);
  99. show_stu(a, N);
  100. return 0;
  101. }


上述案例优化成泛型 :
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define N 3
  6. typedef struct STU
  7. {
  8. int id;
  9. char name[20];
  10. char sex;
  11. int score;
  12. }S, *pS;
  13. int cmp_name(pS a, pS b)
  14. {
  15. return strcmp(a->name, b->name);
  16. }
  17. int cmp_score(pS a, pS b)
  18. {
  19. return a->score - b->score;
  20. }
  21. int cmp_id(pS a, pS b)
  22. {
  23. return a->id - b->id;
  24. }
  25. void sort_stu(pS a, int n, int(*cmp)(pS, pS))
  26. {
  27. int i, j, min;
  28. S temp;
  29. for (i = 0; i < n; ++i)
  30. {
  31. min = i;
  32. for (j = i + 1; j < n; ++j)
  33. if (cmp(&a[min], &a[j]) > 0)
  34. min = j;
  35. temp = a[min];
  36. a[min] = a[i];
  37. a[i] = temp;
  38. }
  39. return;
  40. }
  41. void init_stu(pS a, int n)
  42. {
  43. a[0].id = 50;
  44. strcpy(a[0].name, "liuwei");
  45. a[0].score = 58;
  46. a[0].sex = ‘M‘;
  47. a[1].id = 60;
  48. strcpy(a[1].name, "buanyu");
  49. a[1].score = 40;
  50. a[1].sex = ‘W‘;
  51. a[2].id = 10;
  52. strcpy(a[2].name, "hello");
  53. a[2].score = 63;
  54. a[2].sex = ‘M‘;
  55. }
  56. void show_stu(pS a, int n)
  57. {
  58. int i;
  59. for (i = 0; i < n; ++i)
  60. {
  61. printf("%d\t%s\t%c\t%d\n", a[i].id, a[i].name, a[i].sex, a[i].score);
  62. }
  63. }
  64. int main(void)
  65. {
  66. int n;
  67. struct STU a[N];
  68. init_stu(a, N);
  69. show_stu(a, N);
  70. while (1)
  71. {
  72. printf("请输入以下数字排序:\n0---sort_id\n1---sort_name\n2---sort_score\n");
  73. scanf("%d", &n);
  74. switch (n)
  75. {
  76. case 0:
  77. sort_stu(a, N, cmp_id);
  78. break;
  79. case 1:
  80. sort_stu(a, N, cmp_name);
  81. break;
  82. case 2:
  83. sort_stu(a, N, cmp_score);
  84. break;
  85. }
  86. printf("**************\n");
  87. show_stu(a, N);
  88. }
  89. return 0;
  90. }



函数指针作为参数:【回调函数】
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void say(void)
  4. {
  5. printf("hello world\n");
  6. }
  7. void fun(void(*f)(void))
  8. {
  9. f();
  10. }
  11. int main()
  12. {
  13. fun(say); //传递的是一个函数指针
  14. return 0;
  15. }




/* 
习题:“hello good say oh yeah hello say world xwp xwp hello ...字符串处理
1.统计不重复单词出现的次数
2.按字典序对单词排序
3.按单词出现的频率从高到底排序
4.按单词的长度进行排序
*/
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define N 100
  5. struct STR
  6. {
  7. char * p;
  8. int n;
  9. };
  10. void show(struct STR * a, int n)
  11. {
  12. int i;
  13. for (i = 0; i < n; i++)
  14. {
  15. printf("%s-------%d\n", a[i].p, a[i].n);
  16. }
  17. printf("*************\n");
  18. return;
  19. }
  20. void sort_str(struct STR * a, int n, int(*fun)(struct STR *, struct STR *))
  21. {
  22. int i, j, min;
  23. struct STR temp;
  24. for (i = 0; i < n; i++)
  25. {
  26. min = i;
  27. for (j = i + 1; j < n; j++)
  28. if (fun(&a[min], &a[j]) > 0)
  29. min = j;
  30. temp = a[min];
  31. a[min] = a[i];
  32. a[i] = temp;
  33. }
  34. return;
  35. }
  36. int cmp_dict(struct STR * a, struct STR * b)
  37. {
  38. return strcmp(a->p, b->p);
  39. }
  40. int cmp_times(struct STR * a, struct STR * b)
  41. {
  42. return a->n - b->n;
  43. }
  44. int cmp_len(struct STR * a, struct STR * b)
  45. {
  46. return strlen(a->p) - strlen(b->p);
  47. }
  48. int main()
  49. {
  50. struct STR a[N];
  51. char str[] = "hello hello good say oh yeah hello say world xwp xwp hello";
  52. int m = 1, i = 0, flag = 0;
  53. char * p;
  54. p = strtok(str, " ");
  55. if (p == NULL)
  56. return 1;
  57. a[0].p = p;
  58. a[0].n = 1;
  59. while (p = strtok(NULL, " "))
  60. {
  61. flag = 0;
  62. for (i = 0; i < m; i++)
  63. {
  64. if (strcmp(a[i].p, p) == 0)
  65. {
  66. flag = 1;
  67. a[i].n += 1;
  68. break;
  69. }
  70. }
  71. if (!flag)
  72. {
  73. a[m].p = p;
  74. a[m].n = 1;
  75. m++;
  76. }
  77. }
  78. show(a, m);
  79. sort_str(a, m, cmp_dict);
  80. show(a, m);
  81. sort_str(a, m, cmp_times);
  82. show(a, m);
  83. sort_str(a, m, cmp_len);
  84. show(a, m);
  85. return 0;
  86. }



来自为知笔记(Wiz)


8.4 数组指针 函数指针