The C language implements a level two pointer representing a string array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.