malloc出来的二维数组和二维数组指针做函数参数

定义二维数组:char ** arry = NULL;
变量名做实参:printArry(arry,&depth,&len);
变量名地址做实参:printArry8(&arry,&depth,&len);

接口函数定义:
int printArry(char ** arry,int *depth,int *len)
int printArry8(char *** arry,int *depth,int *len)

#include
#include
#include

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

}

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 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

}

int printArry8(char *** arry,int *depth,int *len){
int mydepth = *depth;
int mylen = *len;
printf(“-----------------\n”);
printf(“%s\n”,func);
for(int i=0;(*arry)[i]!=0;i++) {
printf(“%s\n”,(*arry)[i]);
}
printf(“------------------------------\n”);
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);
printArry8(&arry,&depth,&len);

// freeArry(arry,&depth,&len);
// arry = NULL;//把实参也置空
 freeArry2(&arry,&depth,&len);
freeArry1(&arry,&depth,&len);


return 0;

}

你可能感兴趣的:(c)