二级指针 指针数组

二级指针找出最大的字符 :
  1. #include <stdio.h>
  2. char maxchar(char * str, char** max)
  3. {
  4. char da = *str; //先让最大的执行第一个字符
  5. *max = str;
  6. while (*str)
  7. {
  8. if (*str > da)
  9. {
  10. da = *str;
  11. *max = str;
  12. }
  13. str++;
  14. }
  15. return da;
  16. }
  17. int main()
  18. {
  19. char str[] = "hello liuwxeia";
  20. char * max = NULL;
  21. char ch = maxchar(str, &max);
  22. printf("%p\t%c\n", max, ch);
  23. printf("%p\n", &str);
  24. return 0;
  25. }



指针数组: 数组里的每个元素都是指针

int *a[5];         //a是一个数组,5个元素都是int *d的指针
                       // sizeof(a)     ----------->   20个字节。 因为a是一个数组,不是一个指针
                        //一个指针4个字节

对字符串数组进行排序:
  1. #include <stdio.h>
  2. void show_str(char **s, int n)
  3. {
  4. int i;
  5. for (i = 0; i < n; i++)
  6. {
  7. printf("%s\n", s[i]);
  8. }
  9. }
  10. void sort_str(char **s, int n)
  11. {
  12. int i, j, min;
  13. char * temp;
  14. for (i = 0; i < n; i++)
  15. {
  16. min = i;
  17. for (j = i + 1; j<n; j++)
  18. if (strcmp(s[min], s[j]) > 0)
  19. min = j;
  20. temp = s[min];
  21. s[min] = s[i];
  22. s[i] = temp;
  23. }
  24. }
  25. int main()
  26. {
  27. char *s[] = { "hello", "world", "liuwei", "xuanyuan", "nima" };
  28. show_str(s, 5);
  29. sort_str(s, 5);
  30. printf("*****************\n");
  31. show_str(s, 5);
  32. return 0;
  33. }




ps :参数里二级指针等价于指针数组

 二维数组等价于数组指针





posted @ 2014-09-02 20:37  我爱背单词  阅读(228)  评论(0编辑  收藏  举报