#define _CRT_SECURE_NO_WARNINGS #include"stdio.h" #include"stdlib.h" #include"string.h" void MyPrintf(char **); void MYSORT(char **, int); void SORTBUF(char **); void main() { int i = 0; int j = 0; char *buf; char **myarray; myarray = (char **)malloc(sizeof(char *)* 10); if (myarray == NULL) { return; } for (i = 0; i < 10; i++) { myarray[i] = (char *)malloc(10 * sizeof(char)); if (myarray[i] == NULL) { printf("export err myarray[i]"); return; } sprintf(myarray[i],"%d%d%d",i,i,i); } MyPrintf(myarray); //不调用函数排序 /* for (i = 0; i < 10; i++) { for (j = i + 1; j < 10; j++) { if (strcmp(myarray[i], myarray[j]) < 0) { buf = myarray[i]; myarray[i] = myarray[j]; myarray[j] = buf; } } } */ //互相交换内容//后面的10.没有传进去; SORTBUF(myarray); //MYSORT(myarray, 10);//互相换地址 printf("\n\n\n"); MyPrintf(myarray); system("pause"); } //打印 void MyPrintf(char **p) { char **sortp = p; int i = 0; for (i = 0; i < 10; i++) { printf("%s ", *(sortp + i)); } } //互相换地址; void MYSORT(char **p, int n) { int i = 0; int j = 0; char **temp = NULL; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (strcmp(p[i], p[j]) < 0) { temp = p[i]; p[i] = p[j]; p[j] = temp; } } } } //互相换地址 //互相交换地址里面的值 , void SORTBUF(char **p) { int i = 0, j = 0; char buf[30]; for (i = 0; i < 10; i++) { for (j = i + 1; j < 10; j++) { if (strcmp(p[i], p[j]) < 0) { strcpy(buf, p[i]); strcpy(p[i], p[j]); strcpy(p[j], buf); } } } }
用2种内存模型来排序字符串的的顺序,一种是交换内存地址,第二种是交换内存里面的值;,布布扣,bubuko.com
时间: 2024-10-13 00:48:53