C++二级指针第三种内存模型

时间:2021-03-07 20:54:08
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void main()
{
int i = , j = ;
char buf[];
char **myarray = (char **)malloc(*sizeof(char*)); //int array[10]
if (myarray == NULL)
{
return;
}
for (i=; i<; i++)
{
myarray[i] = (char *)malloc(*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=; i<; i++)
{
printf("%s\n", myarray[i]);
} //排序
for (i=; i<; i++)
{
for (j=i+; j<; j++)
{
if (strcmp(myarray[i], myarray[j]) < )
{
strcpy(buf, myarray[i]);
strcpy(myarray[i], myarray[j]);
strcpy(myarray[j], buf);
}
}
} //第三种内存模型打印
printf("排序之后\n");
for (i=; i<; i++)
{
printf("%s\n", myarray[i]);
} for (i=; i<; 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 = ;
for (i=; i<iNum; i++)
{
printf("%s \n", ArrayStr[i]);
}
return ;
} int sortArrayStr03(char **ArrayStr, int iNum)
{
int i = , j = ;
char *tmp = NULL;
//排序 for (i=; i<iNum; i++)
{
for (j=i+; j<iNum; j++)
{
if (strcmp(ArrayStr[i],ArrayStr[j]) < )
{
tmp = ArrayStr[i];
ArrayStr[i] = ArrayStr[j];
ArrayStr[j] = tmp;
}
}
}
return ;
} void main()
{
int i = , j = ;
char buf[];
char **myarray = (char **)malloc(*sizeof(char*)); //int array[10]
if (myarray == NULL)
{
return;
}
for (i=; i<; i++)
{
myarray[i] = (char *)malloc(*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, ); sortArrayStr03(myarray, ); //第三种内存模型打印
printf("排序之后\n");
printfArr33(myarray, ); for (i=; i<; i++)
{
free(myarray[i] );
}
if (myarray != NULL)
{
free(myarray);
} system("pause");
}