二级指针的使用方法

来源:互联网 发布:网络游戏软件开发 编辑:程序博客网 时间:2024/05/29 02:12


原文:http://blog.csdn.net/bbs375/article/details/52537521


1.二级指针第一种内存模型

[cpp] view plain copy
 print?
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include <string.h>  
  4. #include <ctype.h>  
  5.   
  6. int sort_arr(char**myArr,int num)  
  7. {  
  8.     char*temp;  
  9.     char **p = myArr;  
  10.     int i,j;  
  11.   
  12.     for (i=0;i<num;i++)  
  13.     {  
  14.         for (j=i+1;j<num;j++)  
  15.         {  
  16.             if (strcmp(p[i],p[j])>0)  
  17.             {  
  18.                 temp = p[i];//主意我们交换的只是指针的值,改变指针的指向  
  19.                 p[i] = p[j];  
  20.                 p[j] = temp;  
  21.             }  
  22.         }  
  23.     }  
  24.     return 0;  
  25. }  
  26. int print_arr(char**myArr,int num)  
  27. {  
  28.     int i = 0;  
  29.   
  30.     for (i=0;i<num;i++)  
  31.     {  
  32.         printf("num:%s\n",*(myArr+i));  
  33.     }  
  34.     return 0;  
  35. }  
  36. int main()  
  37. {  
  38.     //数组 数组中的每一个元素是指针,指针数组  
  39.     //1.排序 2.指针做函数参数 3.内存  
  40.     int num,i,j;  
  41.     char*temp;  
  42.     char* myArr[4]={"ddd","cccc","bbbb","aaaaaaa"};  
  43.     print_arr(myArr,4);  
  44.     sort_arr(myArr,4);  
  45.     printf("----------\n");  
  46.     print_arr(myArr,4);  
  47.     system("pause");  
  48.     return 0;  
  49. }  

2.二级指针第二种内存模型

[cpp] view plain copy
 print?
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include <string.h>  
  4. #include <ctype.h>  
  5.   
  6.   
  7. int sort_arr(char myArr[10][20],int num)  
  8. {  
  9.     char temBuf[20];  
  10.     //char **p = myArr;  
  11.     int i,j;  
  12.   
  13.     for (i=0;i<num;i++)  
  14.     {  
  15.         for (j=i+1;j<num;j++)  
  16.         {  
  17.             if (strcmp(myArr[i],myArr[j])>0)  
  18.             {  
  19.                 strcpy(temBuf , myArr[i]);//把数据交换 内存块  
  20.                 strcpy(myArr[i] , myArr[j]);  
  21.                 strcpy(myArr[j] , temBuf);  
  22.             }  
  23.         }  
  24.     }  
  25.     return 0;  
  26. }  
  27. /* 
  28. //第一种内存模型打印函数不适用第二种内存模型 
  29. //原因:二级指针做输入_第一种内存模型 myArr + 1 与第二种内存模型 myArr + 1  
  30. //指针步长不一样哈 指针所指向内存空间的数据不一样 
  31. int print_arr(char**myArr,int num) 
  32. { 
  33.     int i = 0; 
  34.  
  35.     for (i=0;i<num;i++) 
  36.     { 
  37.         printf("num:%s\n",*(myArr+i)); 
  38.     } 
  39.     return 0; 
  40. }*/  
  41.   
  42. int print_arr(char myArr[10][20],int num)  
  43. {  
  44.     int i = 0;  
  45.   
  46.     for (i=0;i<num;i++)  
  47.     {  
  48.         printf("num:%s\n",*(myArr+i));  
  49.     }  
  50.     return 0;  
  51. }  
  52.   
  53. int main()  
  54. {  
  55.     //数组 数组中的每一个元素是指针,指针数组  
  56.     //1.排序 2.指针做函数参数 3.内存  
  57.     char temBuf[30];  
  58.     char myArr[10][20]={"ff","bbbbb","ddddd","ccccc","eeeee"};  
  59.     int num = 5;  
  60.     print_arr(myArr,num);  
  61.       
  62.     sort_arr(myArr,num);  
  63.   
  64.     printf("---------\n");  
  65.     print_arr(myArr,num);  
  66.   
  67.     system("pause");  
  68.     return 0;  
  69. }  

3.二级指针第三种内存模型

[cpp] view plain copy
 print?
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include <string.h>  
  4. #include <ctype.h>  
  5.   
  6. //在堆上分配内存空间 通过return返回  
  7. char** getMen(int num)  
  8. {  
  9.     char **p = NULL;  
  10.     int i,j;  
  11.     p = (char **)malloc(sizeof(char*)*num);  
  12.   
  13.     for (i =0;i<num;i++)  
  14.     {  
  15.         p[i] = (char*)malloc(sizeof(char)*100);  
  16.         sprintf(p[i],"%d%d%d",9-i,9-i,9-i);//向内存空间写入数据  
  17.     }  
  18.     return p;  
  19. }  
  20.   
  21. //在堆上分配内存空间,通过函数参数返回  
  22. int getMen2(char ***p,int num)  
  23. {  
  24.     char **tmp = NULL;  
  25.     int i,j;  
  26.     tmp = (char **)malloc(sizeof(char*)*num);  
  27.     if (p==NULL)  
  28.     {  
  29.         return -1;  
  30.     }  
  31.     for (i =0;i<num;i++)  
  32.     {  
  33.         tmp[i] = (char*)malloc(sizeof(char)*100);  
  34.         sprintf(tmp[i],"%d%d%d",9-i,9-i,9-i);//向内存空间写入数据  
  35.     }  
  36.     *p = tmp;//  int a ; int *p ; p = &a;   *p=10  ;通过三级指针修改二级指针的值  
  37.     return 0;  
  38. }  
  39. //释放内存空间  
  40. int freeMen(char**p,int num)  
  41. {  
  42.     int ret =0,i;  
  43.     for (i =0;i<num;i++)  
  44.     {  
  45.         if (p[i]!=NULL)  
  46.         {  
  47.             free(p[i]);  
  48.             p[i]=NULL;  
  49.         }  
  50.     }  
  51.     if (p!=NULL)  
  52.     {  
  53.         free(p);  
  54.         p=NULL;  
  55.     }  
  56.     return ret;  
  57. }  
  58. //打印  
  59. int print_arr(char**myArr,int num)  
  60. {  
  61.     int i = 0;  
  62.   
  63.     for (i=0;i<num;i++)  
  64.     {  
  65.         printf("num:%s\n",*(myArr+i));  
  66.     }  
  67.     return 0;  
  68. }  
  69. //排序 只交换指针  
  70. int sort_arr(char**myArr,int num)  
  71. {  
  72.     char*temp;  
  73.     char **p = myArr;  
  74.     int i,j;  
  75.   
  76.     for (i=0;i<num;i++)  
  77.     {  
  78.         for (j=i+1;j<num;j++)  
  79.         {  
  80.             if (strcmp(p[i],p[j])>0)  
  81.             {  
  82.                 temp = p[i];//主意我们交换的只是指针的值,改变指针的指向  
  83.                 p[i] = p[j];  
  84.                 p[j] = temp;  
  85.             }  
  86.         }  
  87.     }  
  88.     return 0;  
  89. }  
  90. //排序2 交换数据  
  91. int sort_arr2(char**myArr,int num)  
  92. {  
  93.     char temBuf[20];  
  94.     char **p = myArr;  
  95.     int i,j;  
  96.   
  97.     for (i=0;i<num;i++)  
  98.     {  
  99.         for (j=i+1;j<num;j++)  
  100.         {  
  101.             if (strcmp(p[i],p[j])>0)  
  102.             {  
  103.                 strcpy(temBuf,p[i]);//交换数据  
  104.                 strcpy(p[i] , p[j]);  
  105.                 strcpy(p[j] , temBuf);  
  106.             }  
  107.         }  
  108.     }  
  109.     return 0;  
  110. }  
  111. int main()  
  112. {  
  113.     //1.排序 2.指针做函数参数 3.内存  
  114.     char **p = NULL;  
  115.   
  116.     //p = getMen(5);  
  117.     getMen2(&p,5);  
  118.   
  119.     //排序前打印  
  120.     print_arr(p,5);  
  121.     //排序  
  122.     sort_arr2(p,5);  
  123.     //排序后打印  
  124.     print_arr(p,5);  
  125.     //释放内存  
  126.     freeMen(p,5);  
  127.     p=NULL;  
  128.     system("pause");  
  129.     return 0;  
  130. }  

4.二级指针三种内存模型图:



5.二级指针强化练习(两个辅助指针变量挖字符串)

1)使用第二种内存模型 主调函数分配内存

[cpp] view plain copy
 print?
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include <string.h>  
  4. #include <ctype.h>  
  5. /*函数功能 根据字符c来分割字符串str*/  
  6. int splitStr(const char *str,char c ,char buf[10][20],int *num)  
  7. {  
  8.     char* p = NULL,*pTmp = NULL;//两个辅助指针变量  
  9.     int tmpcount = 0,len;  
  10.     p = str;  
  11.     pTmp = str;  
  12.     do   
  13.     {  
  14.         p = strchr(p,c);  
  15.           
  16.         if (p!=NULL)  
  17.         {  
  18.             if (p-pTmp>0)  
  19.             {  
  20.                 strncpy(buf[tmpcount],pTmp,p-pTmp);  
  21.                 buf[tmpcount][p-pTmp] = '\0';  
  22.                 //printf("%s\n",buf[tmpcount]);  
  23.                 tmpcount++;  
  24.                 pTmp = p = p+1;  
  25.                 len = strlen(p);//用来保存最后一个字符串的长度  
  26.             }     
  27.         }  
  28.         else  
  29.         {  
  30.             //拷贝最后一个分割的字符串包括\0  
  31.             strncpy(buf[tmpcount],pTmp,len+1);  
  32.             break;  
  33.         }  
  34.           
  35.     } while (*p!='\0');  
  36.     *num=tmpcount+1;  
  37.     return 0;  
  38. }  
  39. /*函数功能 打印二维数组*/  
  40.   
  41. void printArr(char a[10][20],int n)  
  42. {  
  43.     int i;  
  44.     for (i=0;i<n;i++)  
  45.     {  
  46.         printf("%s\n",*(a+i));  
  47.     }  
  48. }  
  49.   
  50. int main()  
  51. {  
  52.     char *input="abcdefg,hjkln,sssss,kkk,hhh,j";  
  53.     char ctemp = ',';  
  54.     char myArr[10][20]={0};  
  55.     char *tmp;  
  56.       
  57.     int ret;  
  58.     int n,i;  
  59.       
  60.     ret = splitStr(input,ctemp,myArr,&n);  
  61.     if (ret!=0)  
  62.     {  
  63.         printf("error\n");  
  64.     }  
  65.     printArr(myArr,n);  
  66.       
  67.     system("pause");  
  68.     return 0;  
  69. }  

2)使用第三种内存模型 主调函数分配内存

[cpp] view plain copy
 print?
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include <string.h>  
  4. #include <ctype.h>  
  5.   
  6. /*第三种内存模型*/  
  7. int splitStr(const char *str,char c,char **buf,int *count)  
  8. {  
  9.   
  10.     char* p = NULL,*pTmp = NULL;//两个辅助指针变量  
  11.     int tmpcount = 0,len;  
  12.     p = str;  
  13.     pTmp = str;  
  14.     do   
  15.     {  
  16.         p = strchr(p,c);  
  17.   
  18.         if (p!=NULL)  
  19.         {  
  20.             if (p-pTmp>0)  
  21.             {  
  22.                 strncpy(buf[tmpcount],pTmp,p-pTmp);  
  23.                 buf[tmpcount][p-pTmp] = '\0';  
  24.                 //printf("%s\n",buf[tmpcount]);  
  25.                 tmpcount++;  
  26.                 pTmp = p = p+1;  
  27.                 len = strlen(p);//用来保存最后一个字符串的长度  
  28.             }     
  29.         }  
  30.         else  
  31.         {  
  32.             //拷贝最后一个分割的字符串包括\0  
  33.             strncpy(buf[tmpcount],pTmp,len+1);  
  34.             break;  
  35.         }     
  36.     } while (*p!='\0');  
  37.     *count=tmpcount+1;  
  38.     return 0;  
  39. }  
  40.   
  41. int main()  
  42. {  
  43.     char *input="abcdefg,hjkln,sssss,kkk,hhh,j";  
  44.     char ctemp = ',';  
  45.     char **p = NULL;  
  46.     int ret;  
  47.     int n,i;  
  48.     p=(char**)malloc(10*sizeof(char*));  
  49.     if (p==NULL)  
  50.     {  
  51.         return;  
  52.     }  
  53.     for (i=0;i<10;i++)  
  54.     {  
  55.         p[i] = (char *)malloc(20*sizeof(char));  
  56.     }  
  57.     ret = splitStr(input,ctemp,p,&n);  
  58.     if (ret!=0)  
  59.     {  
  60.         printf("error\n");  
  61.     }  
  62.     for (i=0;i<n;i++)  
  63.     {  
  64.         printf("%s\n",*(p+i));  
  65.     }  
  66.          //释放内存  
  67.     system("pause");  
  68.     return 0;  
  69. }  

3)使用第三种内存模型 被调函数分配内存 通过return返回

[cpp] view plain copy
 print?
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include <string.h>  
  4. #include <ctype.h>  
  5. /*第三种内存模型 被调函数分配内存 通过return返回*/  
  6. char** splitStr2(char *str,char c,int *count)  
  7. {  
  8.     char* p = NULL,*pTmp = NULL;//两个辅助指针变量  
  9.     int tmpcount = 0,len,len2;  
  10.     char**buf;  
  11.     p = str;  
  12.     pTmp = str;  
  13.       
  14.     //第一遍扫描 开辟第一维空间  
  15.     do   
  16.     {  
  17.         p = strchr(p,c);  
  18.         if (p!=NULL)  
  19.         {  
  20.             if (p-pTmp>0)  
  21.             {     
  22.                 tmpcount++;  
  23.                 pTmp = p = p+1;  
  24.             }     
  25.         }  
  26.         else  
  27.         {  
  28.             break;  
  29.         }     
  30.     } while (*p!='\0');  
  31.     *count=tmpcount+1;  
  32.     buf = (char**)malloc((tmpcount+1)*sizeof(char*));  
  33.     //printf("tmpcount:%d\n",tmpcount);  
  34.     if(buf==NULL)  
  35.     {  
  36.         return NULL;  
  37.     }  
  38.   
  39.     //第二遍扫描 根据分割的字符串的长度,开辟第二维空间并拷贝字符串  
  40.     tmpcount = 0;  
  41.     p = str;  
  42.     pTmp = str;  
  43.     do   
  44.     {  
  45.         p = strchr(p,c);  
  46.         if (p!=NULL)  
  47.         {  
  48.             if (p-pTmp>0)  
  49.             {     
  50.                 len = p-pTmp+1;  
  51.                 buf[tmpcount] = (char*)malloc(len*sizeof(char));  
  52.                 if (buf[tmpcount] == NULL)  
  53.                 {  
  54.                     free(buf);  
  55.                     return ;  
  56.                 }  
  57.                 strncpy(buf[tmpcount],pTmp,p-pTmp);  
  58.                 buf[tmpcount][p-pTmp] = '\0';  
  59.                 tmpcount++;  
  60.                 pTmp = p = p+1;  
  61.                 len2 = strlen(p);//用来保存最后一个字符串的长度  
  62.             }     
  63.         }  
  64.         else  
  65.         {  
  66.             //printf("tmpcount:%d\n",tmpcount);  
  67.             //printf("len2:%d\n",len2);  
  68.             buf[tmpcount] = (char*)malloc((len2+1)*sizeof(char));  
  69.             if (buf[tmpcount] == NULL)  
  70.             {  
  71.                 free(buf);  
  72.                 return ;  
  73.             }  
  74.             //拷贝最后一个分割的字符串包括\0  
  75.             strncpy(buf[tmpcount],pTmp,len2+1);  
  76.             break;  
  77.         }     
  78.     } while (*p!='\0');  
  79.     return buf;  
  80. }  
  81.   
  82. int main()  
  83. {  
  84.     char *input="abcdefg,hjkln,sssss,kkk,hhh,j";  
  85.     char ctemp = ',';  
  86.     char **p = NULL;  
  87.     int ret;  
  88.     int n,i;  
  89.     p = splitStr2(input,ctemp,&n);  
  90.     printf("n:%d\n",n);  
  91.     for (i=0;i<n;i++)  
  92.     {  
  93.         printf("%s\n",*(p+i));  
  94.       
  95.     }  
  96.      //释放内存空间  
  97.      system("pause");  
  98.     return 0;  
  99. }  

4)使用第三种内存模型 被调函数分配内存 通过函数参数返回

[cpp] view plain copy
 print?
  1. <pre name="code" class="cpp">#include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include <string.h>  
  4. #include <ctype.h>  
  5. /*第三种内存模型 被调函数分配内存 通过tmpbuf返回*/  
  6.  int splitStr3(char *str,char c,char*** tmpbuf,int *count)  
  7. {  
  8.     char* p = NULL,*pTmp = NULL;//两个辅助指针变量  
  9.     int tmpcount = 0,len,len2;  
  10.     char**buf;  
  11.     p = str;  
  12.     pTmp = str;  
  13.   
  14.     //第一遍扫描 开辟第一维空间  
  15.     do   
  16.     {  
  17.         p = strchr(p,c);  
  18.         if (p!=NULL)  
  19.         {  
  20.             if (p-pTmp>0)  
  21.             {      
  22.                 tmpcount++;  
  23.                 pTmp = p = p+1;  
  24.             }      
  25.         }  
  26.         else  
  27.         {  
  28.             break;  
  29.         }      
  30.     } while (*p!='\0');  
  31.     *count=tmpcount+1;  
  32.     buf = (char**)malloc((tmpcount+1)*sizeof(char*));  
  33.     //printf("tmpcount:%d\n",tmpcount);  
  34.     if(buf==NULL)  
  35.     {  
  36.         return -1;  
  37.     }  
  38.   
  39.     //第二遍扫描 根据分割的字符串的长度,开辟第二维空间并拷贝字符串  
  40.     tmpcount = 0;  
  41.     p = str;  
  42.     pTmp = str;  
  43.     do   
  44.     {  
  45.         p = strchr(p,c);  
  46.         if (p!=NULL)  
  47.         {  
  48.             if (p-pTmp>0)  
  49.             {      
  50.                 len = p-pTmp+1;  
  51.                 buf[tmpcount] = (char*)malloc(len*sizeof(char));  
  52.                 if (buf[tmpcount] == NULL)  
  53.                 {  
  54.                     free(buf);  
  55.                     return -2;  
  56.                 }  
  57.                 strncpy(buf[tmpcount],pTmp,p-pTmp);  
  58.                 buf[tmpcount][p-pTmp] = '\0';  
  59.                 tmpcount++;  
  60.                 pTmp = p = p+1;  
  61.                 len2 = strlen(p);//用来保存最后一个字符串的长度  
  62.             }      
  63.         }  
  64.         else  
  65.         {  
  66.             //printf("tmpcount:%d\n",tmpcount);  
  67.             //printf("len2:%d\n",len2);  
  68.             buf[tmpcount] = (char*)malloc((len2+1)*sizeof(char));  
  69.             if (buf[tmpcount] == NULL)  
  70.             {  
  71.                 free(buf);  
  72.                 return -2;  
  73.             }  
  74.             //拷贝最后一个分割的字符串包括\0  
  75.             strncpy(buf[tmpcount],pTmp,len2+1);  
  76.             break;  
  77.         }      
  78.     } while (*p!='\0');  
  79.     *tmpbuf = buf;  
  80.     return 0;  
  81. }  
  82.  //释放内存空间  
  83.  void freeMem(char**p,int count)  
  84.  {  
  85.      int i =0;  
  86.      if (p == NULL)  
  87.      {  
  88.          return ;  
  89.      }  
  90.      for (i=0;i<count;i++)  
  91.      {  
  92.          if (p[i]!=NULL)  
  93.          {  
  94.              free(p[i]);  
  95.          }  
  96.      }  
  97.      if (p!=NULL)  
  98.      {  
  99.          free(p);  
  100.      }  
  101.  }  
  102. int main()  
  103. {  
  104.     char *input="abcdefg,hjkln,sssss,kkk,hhh,j";  
  105.     char ctemp = ',';  
  106.     char **p = NULL;  
  107.     int ret;  
  108.     int n,i;  
  109.     ret = splitStr3(input,ctemp,&p,&n);  
  110.     if (ret!= 0)  
  111.     {  
  112.         printf("error");  
  113.         return -1;  
  114.     }  
  115.     //printf("n:%d\n",n);  
  116.     for (i=0;i<n;i++)  
  117.     {  
  118.         printf("%s\n",*(p+i));  
  119.       
  120.     }  
  121.     //释放内存  
  122.     freeMem(p,n);  
  123.     system("pause");  
  124.     return 0;  
  125. }