Header file:
#include <stdlib.h>#include<stdio.h>#include<string.h>
Function Prototypes:
Char* * Createbuff (Char**buff,intArraylength,intCharlength);//Create a level two pointervoidInitdemo (Char**buff,intArraylength);//initializing a level two pointervoidDestorybuff (Char**buff,intArraylength);//destroying level two pointersvoidPrintbuff (Char* * Buff,intArraylength);//traverse level Two pointersvoidSortbuffarray (Char* * Buff,intArraylength);//Sort
Implementation functions:
1 Char* * Createbuff (Char**buff,intArraylength,intcharlength) {2 3 inti;4 5Buff = (Char**)malloc(sizeof(Char*) *arraylength); Allocate space to *buff, the buff points to its first address6 7 for(i =0; i < arraylength; ++i) {8 9Buff[i] = (Char*)malloc(sizeof(Char) *charlength); *buff[i] Allocate space, Buff[i] point to its first addressTen One } A - returnBuff; - } the - voidInitdemo (Char**buff,intarraylength) { - - inti; + - for(i =0; i < arraylength; ++i) { + Asprintf (Buff[i],"%d%d%d", i +1, i +1, i +1); at - } - } - - voidDestorybuff (Char**buff,intarraylength) { - in inti; - to for(i =0; i < arraylength; ++i) { + - if(Buff[i]! =NULL) { the * Free(Buff[i]); $ Panax NotoginsengBuff[i] =NULL; - the } + A } the + if(Buff! =NULL) { - $ Free(buff); $ -Buff =NULL; - the } - }Wuyi the - Wu voidPrintbuff (Char* * Buff,intarraylength) { - About inti; $ - for(i =0; i < arraylength; ++i) { -printf"%s\n", Buff[i]); - } A } + the voidSortbuffarray (Char* * Buff,intarraylength) { - $ intI, J; the the Char*temp =NULL; the the for(i =0; i < arraylength; ++i) { - in for(j = i +1; J < Arraylength; ++j) { the the if(strcmp (Buff[j], buff[i]) >0){ About thetemp =Buff[i]; the theBuff[i] =Buff[j]; + -BUFF[J] =temp; the Bayi } the the } - - } the the}
Test:
voidMain () {intArraylength =5, Charlength = -; Char**buff =NULL; Buff=createbuff (Buff, arraylength, charlength); Initdemo (buff, arraylength); printf ("sort before \ n"); Printbuff (buff, arraylength); Sortbuffarray (buff, arraylength); printf ("sort after \ n"); PrintArray (buff, arraylength); Destorybuff (buff, arraylength); System ("Pause");}
Attention:
A char **buff variable cannot be passed as an argument to a formal parameter in the Createbuff function, so that the end of the Createbuff function call frees the parameter buff, so it can only be returned as a return value or, a three-level pointer is defined in the function, and the address of the buff is passed in when called.
The C language implements a level two pointer representing a string array