1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<string.h> 5 6 void printNum(char **array, int num) 7 { 8 int i = 0; 9 for (i = 0; i < num; i++) 10 { 11 printf("%s ", array[i]); 12 } 13 } 14 15 void sortNum(char **array,int num) 16 { 17 char *tmp = NULL; 18 int i = 0, j = 0; 19 for (i = 0; i < num; i++) 20 { 21 for (j = i; j < num; j++) 22 { 23 if (strcmp(array[i], array[j])>0) 24 { 25 tmp = array[i]; //注意:交換的數據的值,交換的是指針 //改變的是指針的指向 26 array[i] = array[j]; 27 array[j] = tmp; 28 } 29 } 30 } 31 } 32 33 int main() 34 { 35 char *array[] = {"abbbb","aaaa","vvv","nnnn"}; 36 37 int num = 0; 38 char *tmp; 39 num = sizeof(array) / sizeof(array[0]); 40 printf("排序前:"); 41 printNum(array,num); 42 sortNum(array,num); 43 printf("排序後:"); 44 printNum(array, num); 45 46 system("pause"); 47 return 0; 48 }