三种内存模型-第三种
//打印字符串
void printstr(char **buf, int nsize)
{
for (int i = 0; i < nsize; i++)
{
printf("%s ", buf[i]);
}
}
//冒泡排序
void sortstr(char **buf, int nsize)
{
for (int i = 0; i < nsize - 1; i++)
{
for (int j = 0; j < nsize - i - 1; j++)
{
if (strcmp(buf[j], buf[j + 1]) < 0)
{
char *temp = buf[j];
buf[j] = buf[j + 1];
buf[j + 1] = temp;
}
}
}
}
void main()
{
//申请8个指针类型的数组
char **m_tyarry = (char **)malloc(sizeof(char *) * 8);
//给每一个指针分配空间,用于存储数据
for (int i = 0; i < 8;i++)
{
m_tyarry[i] = (char *)malloc(24 * sizeof(char));
memset(m_tyarry[i], 0, 24 * sizeof(char));
sprintf(m_tyarry[i],"%d%d%d",i+1,i+1,i+1);
}
printstr(m_tyarry,8);
printf("\n从大到小排序之后:\n");
sortstr(m_tyarry, 8);
printstr(m_tyarry, 8);
printf("\n");
system("pause");
}
结果: