方法一:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getmem(char *** parry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
char ** myarry = (char **)malloc(mydepth*sizeof(char*));
for(int i=0;i<mydepth;i++) {
myarry[i] = (char *)malloc(mylen*sizeof(char));
}
*parry = myarry;
return 0;
}
int sortArry(char ** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
char temp[mylen];
for(int i = 0;i<mydepth;i++) {
for(int j=i+1;j<mydepth;j++)
if(strcmp(arry[i],arry[j])>0) {
strcpy(temp,arry[i]);
strcpy(arry[i],arry[j]);
strcpy(arry[j],temp);
}
}
return 0;
}
int sortArry1(char ** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
char *temp;
for(int i = 0;i<mydepth;i++) {
for(int j=i+1;j<mydepth;j++)
if(strcmp(arry[i],arry[j])<0) {
char * temp = arry[i];
arry[i] = arry[j];
arry[j] = temp;
}
}
return 0;
}
int initArry(char ** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
for(int i=0;i<mydepth;i++) {
strcpy(arry[i],"aaaaaaaa");
}
strcpy(arry[1],"cccccccc");
strcpy(arry[3],"bbbbbbbb");
return 0;
}
int printArry(char ** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
for(int i=0;i<mydepth;i++) {
printf(“%s\n”,arry[i]);
}
printf(“------------------------------\n”);
return 0;
}
int freeArry(char ** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
if(arry != NULL) {
for(int i=0;i<mydepth;i++) {
if(arry[i] !=NULL ) {
free(arry[i]);
}
}
free(arry);
}
return 0;
}
int freeArry1(char *** marry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
char ** arry = *marry;
if(arry != NULL) {
for(int i=0;i<mydepth;i++) {
if(arry[i] !=NULL ) {
free(arry[i]);
}
}
free(arry);
}
marry = NULL;
return 0;
}
int freeArry2(char *** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
if(arry != NULL) {
for(int i=0;i<mydepth;i++) {
if((*arry)[i] !=NULL ) {
free((*arry)[i]);
}
}
free(*arry);
}
arry = NULL;
return 0;
}
int main(){
int depth = 5;
int len = 100;
char ** arry = NULL;
getmem(&arry,&depth,&len);
initArry(arry,&depth,&len);
printArry(arry,&depth,&len);
sortArry(arry,&depth,&len);
printArry(arry,&depth,&len);
sortArry1(arry,&depth,&len);
printArry(arry,&depth,&len);
//freeArry(arry,&depth,&len);
//freeArry1(&arry,&depth,&len);
freeArry2(&arry,&depth,&len);
return 0;
}
方法二:
多分配一个数组,把最后一个数组置为0,作为循环判断条件。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int getmem(char *** parry,int *depth,int *len){
int mydepth = *depth+1;
int mylen = *len;
char ** myarry = (char **)malloc(mydepth*sizeof(char*));
for(int i=0;i<mydepth;i++) {
myarry[i] = (char *)malloc(mylen*sizeof(char));
}
*parry = myarry;
return 0;
}
int sortArry(char ** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
char temp[mylen];
for(int i = 0;arry[i]!=0;i++) {
for(int j=i+1;arry[j]!=0;j++)
if(strcmp(arry[i],arry[j])>0) {
strcpy(temp,arry[i]);
strcpy(arry[i],arry[j]);
strcpy(arry[j],temp);
}
}
return 0;
}
int sortArry1(char ** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
char *temp;
for(int i = 0;arry[i]!=0;i++) {
for(int j=i+1;arry[j]!=0;j++)
if(strcmp(arry[i],arry[j])<0) {
char * temp = arry[i];
arry[i] = arry[j];
arry[j] = temp;
}
}
return 0;
}
int initArry(char ** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
int i=0;
for(i=0;i<mydepth;i++) {
strcpy(arry[i],“aaaaaaaa”);
}
strcpy(arry[1],“cccccccc”);
strcpy(arry[3],“bbbbbbbb”);
arry[i]=0;
return 0;
}
int printArry(char ** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
for(int i=0;arry[i]!=0;i++) {
printf(“%s\n”,arry[i]);
}
printf(“------------------------------\n”);
return 0;
}
int freeArry(char ** arry,int *depth,int *len){
int mydepth = *depth+1;
int mylen = *len;
if(arry != NULL) {
for(int i=0;arry[i]!=0;i++) {
if(arry[i] !=NULL ) {
free(arry[i]);
arry[i]=NULL;
}
}
free(arry);
arry=NULL;//把形参置空
}
return 0;
}
int freeArry1(char *** marry,int *depth,int *len){
int mydepth = *depth+1;
int mylen = *len;
char ** arry = *marry;
if(arry != NULL) {
for(int i=0;arry[i]!=0;i++) {
if(arry[i] !=NULL ) {
free(arry[i]);
arry[i]=NULL;
}
}
free(arry);//把形参置空
arry=NULL;
}
marry = NULL;//把实参也置空
return 0;
}
int freeArry2(char *** arry,int *depth,int *len){
int mydepth = *depth+1;
int mylen = *len;
if((*arry) != NULL) {
for(int i=0;i<mydepth;i++) {
if((*arry)[i] !=NULL ) {
free((*arry)[i]);
(*arry)[i]=NULL;
}
}
free(*arry);
*arry=NULL;//把形参置空
}
arry = NULL;//把实参也置空
return 0;
}
int main(){
int depth = 8;
int len = 100;
char ** arry = NULL;
getmem(&arry,&depth,&len);
initArry(arry,&depth,&len);
printArry(arry,&depth,&len);
sortArry(arry,&depth,&len);
printArry(arry,&depth,&len);
sortArry1(arry,&depth,&len);
printArry(arry,&depth,&len);
freeArry(arry,&depth,&len);
arry = NULL;//把实参也置空
freeArry1(&arry,&depth,&len);
freeArry2(&arry,&depth,&len);
return 0;
}