二维数组,交换的是内存块
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void printMyArray22(char myArray[10][30], int num)
{
int i = 0;
for (i = 0; i < num; i++)
{
//printf("%s \n",myArray[i]);
printf("%s \n", *(myArray + i));
}
}
void sortMyArray22(char myArray[10][30], int num)
{
int i = 0, j = 0;
char tmpbuf[30];
//排序
for (i = 0; i < num; i++)
{
for (j = i + 1; j < num; j++)
{
if (strcmp(myArray[i], myArray[j]) > 0)
{
strcpy(tmpbuf, myArray[i]);
strcpy(myArray[i], myArray[j]);//交换的是内存块
strcpy(myArray[j], tmpbuf);
}
}
}
}
int main01()
{
int i = 0,j=0;
int num = 4;
char mybuf[30];
char tmpbuf[30];
char myArray[10][30] = {"aaaa","ccccc","bbbbb","111111111"};
//打印
/*for (i=0;i<num;i++)
{
printf("%s \n",myArray[i]);
}*/
printf("排序之前\n");
printMyArray22(myArray, num);
//排序
/*for (i=0;i<num;i++)
{
for (j=i+1;j<num;j++)
{
if (strcmp(myArray[i], myArray[j]) > 0)
{
strcpy(tmpbuf, myArray[i]);
strcpy(myArray[i], myArray[j]);
strcpy(myArray[j], tmpbuf);
}
}
}*/
sortMyArray22(myArray, num);
printf("排序之后\n");
for (i = 0; i < num; i++)
{
printf("%s \n", myArray[i]);
}
printf("hello... \n");
return 0;
}