#include "stdio.h" #include "stdlib.h" #include "string.h" void main() { int i = 0, j = 0; char buf[100]; char **myarray = (char **)malloc(10*sizeof(char*)); //int array[10] if (myarray == NULL) { return; } for (i=0; i<10; i++) { myarray[i] = (char *)malloc(100*sizeof(char)); //char buf[100]; if (myarray[i] == NULL) { printf("ddddde "); return; } sprintf(myarray[i],"%d%d%d ", i, i, i); } //第三种内存模型打印 printf("排序之前 "); for (i=0; i<10; i++) { printf("%s ", myarray[i]); } //排序 for (i=0; i<10; i++) { for (j=i+1; j<10; j++) { if (strcmp(myarray[i], myarray[j]) < 0) { strcpy(buf, myarray[i]); strcpy(myarray[i], myarray[j]); strcpy(myarray[j], buf); } } } //第三种内存模型打印 printf("排序之后 "); for (i=0; i<10; i++) { printf("%s ", myarray[i]); } for (i=0; i<10; i++) { free(myarray[i] ); } if (myarray != NULL) { free(myarray); } system("pause"); }
#include "stdio.h" #include "stdlib.h" #include "string.h" int printfArr33(char **ArrayStr, int iNum) { int i = 0; for (i=0; i<iNum; i++) { printf("%s ", ArrayStr[i]); } return 0; } int sortArrayStr03(char **ArrayStr, int iNum) { int i = 0, j = 0; char *tmp = NULL; //排序 for (i=0; i<iNum; i++) { for (j=i+1; j<iNum; j++) { if (strcmp(ArrayStr[i],ArrayStr[j]) < 0) { tmp = ArrayStr[i]; ArrayStr[i] = ArrayStr[j]; ArrayStr[j] = tmp; } } } return 0; } void main() { int i = 0, j = 0; char buf[100]; char **myarray = (char **)malloc(10*sizeof(char*)); //int array[10] if (myarray == NULL) { return; } for (i=0; i<10; i++) { myarray[i] = (char *)malloc(100*sizeof(char)); //char buf[100]; if (myarray[i] == NULL) { printf("ddddde "); return; } sprintf(myarray[i],"%d%d%d ", i, i, i); } //第三种内存模型打印 printf("排序之前 "); printfArr33(myarray, 10); sortArrayStr03(myarray, 10); //第三种内存模型打印 printf("排序之后 "); printfArr33(myarray, 10); for (i=0; i<10; i++) { free(myarray[i] ); } if (myarray != NULL) { free(myarray); } system("pause"); }