#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\n"); return; } sprintf(myarray[i],"%d%d%d ", i, i, i); } //第三种内存模型打印 printf("排序之前\n"); for (i=0; i<10; i++) { printf("%s\n", 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("排序之后\n"); for (i=0; i<10; i++) { printf("%s\n", 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 \n", 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\n"); return; } sprintf(myarray[i],"%d%d%d ", i, i, i); } //第三种内存模型打印 printf("排序之前\n"); printfArr33(myarray, 10); sortArrayStr03(myarray, 10); //第三种内存模型打印 printf("排序之后\n"); printfArr33(myarray, 10); for (i=0; i<10; i++) { free(myarray[i] ); } if (myarray != NULL) { free(myarray); } system("pause"); }
时间: 2024-11-02 22:43:46