二级指针的使用方法
来源:互联网 发布:网络游戏软件开发 编辑:程序博客网 时间:2024/05/29 02:12
原文:http://blog.csdn.net/bbs375/article/details/52537521
1.二级指针第一种内存模型
[cpp] view plain copy print?
- #include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int sort_arr(char**myArr,int num)
- {
- char*temp;
- char **p = myArr;
- int i,j;
- for (i=0;i<num;i++)
- {
- for (j=i+1;j<num;j++)
- {
- if (strcmp(p[i],p[j])>0)
- {
- temp = p[i];//主意我们交换的只是指针的值,改变指针的指向
- p[i] = p[j];
- p[j] = temp;
- }
- }
- }
- return 0;
- }
- int print_arr(char**myArr,int num)
- {
- int i = 0;
- for (i=0;i<num;i++)
- {
- printf("num:%s\n",*(myArr+i));
- }
- return 0;
- }
- int main()
- {
- //数组 数组中的每一个元素是指针,指针数组
- //1.排序 2.指针做函数参数 3.内存
- int num,i,j;
- char*temp;
- char* myArr[4]={"ddd","cccc","bbbb","aaaaaaa"};
- print_arr(myArr,4);
- sort_arr(myArr,4);
- printf("----------\n");
- print_arr(myArr,4);
- system("pause");
- return 0;
- }
2.二级指针第二种内存模型
[cpp] view plain copy print?
- #include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- #include <ctype.h>
- int sort_arr(char myArr[10][20],int num)
- {
- char temBuf[20];
- //char **p = myArr;
- int i,j;
- for (i=0;i<num;i++)
- {
- for (j=i+1;j<num;j++)
- {
- if (strcmp(myArr[i],myArr[j])>0)
- {
- strcpy(temBuf , myArr[i]);//把数据交换 内存块
- strcpy(myArr[i] , myArr[j]);
- strcpy(myArr[j] , temBuf);
- }
- }
- }
- return 0;
- }
- /*
- //第一种内存模型打印函数不适用第二种内存模型
- //原因:二级指针做输入_第一种内存模型 myArr + 1 与第二种内存模型 myArr + 1
- //指针步长不一样哈 指针所指向内存空间的数据不一样
- int print_arr(char**myArr,int num)
- {
- int i = 0;
- for (i=0;i<num;i++)
- {
- printf("num:%s\n",*(myArr+i));
- }
- return 0;
- }*/
- int print_arr(char myArr[10][20],int num)
- {
- int i = 0;
- for (i=0;i<num;i++)
- {
- printf("num:%s\n",*(myArr+i));
- }
- return 0;
- }
- int main()
- {
- //数组 数组中的每一个元素是指针,指针数组
- //1.排序 2.指针做函数参数 3.内存
- char temBuf[30];
- char myArr[10][20]={"ff","bbbbb","ddddd","ccccc","eeeee"};
- int num = 5;
- print_arr(myArr,num);
- sort_arr(myArr,num);
- printf("---------\n");
- print_arr(myArr,num);
- system("pause");
- return 0;
- }
3.二级指针第三种内存模型
[cpp] view plain copy print?
- #include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- #include <ctype.h>
- //在堆上分配内存空间 通过return返回
- char** getMen(int num)
- {
- char **p = NULL;
- int i,j;
- p = (char **)malloc(sizeof(char*)*num);
- for (i =0;i<num;i++)
- {
- p[i] = (char*)malloc(sizeof(char)*100);
- sprintf(p[i],"%d%d%d",9-i,9-i,9-i);//向内存空间写入数据
- }
- return p;
- }
- //在堆上分配内存空间,通过函数参数返回
- int getMen2(char ***p,int num)
- {
- char **tmp = NULL;
- int i,j;
- tmp = (char **)malloc(sizeof(char*)*num);
- if (p==NULL)
- {
- return -1;
- }
- for (i =0;i<num;i++)
- {
- tmp[i] = (char*)malloc(sizeof(char)*100);
- sprintf(tmp[i],"%d%d%d",9-i,9-i,9-i);//向内存空间写入数据
- }
- *p = tmp;// int a ; int *p ; p = &a; *p=10 ;通过三级指针修改二级指针的值
- return 0;
- }
- //释放内存空间
- int freeMen(char**p,int num)
- {
- int ret =0,i;
- for (i =0;i<num;i++)
- {
- if (p[i]!=NULL)
- {
- free(p[i]);
- p[i]=NULL;
- }
- }
- if (p!=NULL)
- {
- free(p);
- p=NULL;
- }
- return ret;
- }
- //打印
- int print_arr(char**myArr,int num)
- {
- int i = 0;
- for (i=0;i<num;i++)
- {
- printf("num:%s\n",*(myArr+i));
- }
- return 0;
- }
- //排序 只交换指针
- int sort_arr(char**myArr,int num)
- {
- char*temp;
- char **p = myArr;
- int i,j;
- for (i=0;i<num;i++)
- {
- for (j=i+1;j<num;j++)
- {
- if (strcmp(p[i],p[j])>0)
- {
- temp = p[i];//主意我们交换的只是指针的值,改变指针的指向
- p[i] = p[j];
- p[j] = temp;
- }
- }
- }
- return 0;
- }
- //排序2 交换数据
- int sort_arr2(char**myArr,int num)
- {
- char temBuf[20];
- char **p = myArr;
- int i,j;
- for (i=0;i<num;i++)
- {
- for (j=i+1;j<num;j++)
- {
- if (strcmp(p[i],p[j])>0)
- {
- strcpy(temBuf,p[i]);//交换数据
- strcpy(p[i] , p[j]);
- strcpy(p[j] , temBuf);
- }
- }
- }
- return 0;
- }
- int main()
- {
- //1.排序 2.指针做函数参数 3.内存
- char **p = NULL;
- //p = getMen(5);
- getMen2(&p,5);
- //排序前打印
- print_arr(p,5);
- //排序
- sort_arr2(p,5);
- //排序后打印
- print_arr(p,5);
- //释放内存
- freeMen(p,5);
- p=NULL;
- system("pause");
- return 0;
- }
4.二级指针三种内存模型图:
5.二级指针强化练习(两个辅助指针变量挖字符串)
1)使用第二种内存模型 主调函数分配内存
[cpp] view plain copy print?
- #include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- #include <ctype.h>
- /*函数功能 根据字符c来分割字符串str*/
- int splitStr(const char *str,char c ,char buf[10][20],int *num)
- {
- char* p = NULL,*pTmp = NULL;//两个辅助指针变量
- int tmpcount = 0,len;
- p = str;
- pTmp = str;
- do
- {
- p = strchr(p,c);
- if (p!=NULL)
- {
- if (p-pTmp>0)
- {
- strncpy(buf[tmpcount],pTmp,p-pTmp);
- buf[tmpcount][p-pTmp] = '\0';
- //printf("%s\n",buf[tmpcount]);
- tmpcount++;
- pTmp = p = p+1;
- len = strlen(p);//用来保存最后一个字符串的长度
- }
- }
- else
- {
- //拷贝最后一个分割的字符串包括\0
- strncpy(buf[tmpcount],pTmp,len+1);
- break;
- }
- } while (*p!='\0');
- *num=tmpcount+1;
- return 0;
- }
- /*函数功能 打印二维数组*/
- void printArr(char a[10][20],int n)
- {
- int i;
- for (i=0;i<n;i++)
- {
- printf("%s\n",*(a+i));
- }
- }
- int main()
- {
- char *input="abcdefg,hjkln,sssss,kkk,hhh,j";
- char ctemp = ',';
- char myArr[10][20]={0};
- char *tmp;
- int ret;
- int n,i;
- ret = splitStr(input,ctemp,myArr,&n);
- if (ret!=0)
- {
- printf("error\n");
- }
- printArr(myArr,n);
- system("pause");
- return 0;
- }
2)使用第三种内存模型 主调函数分配内存
[cpp] view plain copy print?
- #include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- #include <ctype.h>
- /*第三种内存模型*/
- int splitStr(const char *str,char c,char **buf,int *count)
- {
- char* p = NULL,*pTmp = NULL;//两个辅助指针变量
- int tmpcount = 0,len;
- p = str;
- pTmp = str;
- do
- {
- p = strchr(p,c);
- if (p!=NULL)
- {
- if (p-pTmp>0)
- {
- strncpy(buf[tmpcount],pTmp,p-pTmp);
- buf[tmpcount][p-pTmp] = '\0';
- //printf("%s\n",buf[tmpcount]);
- tmpcount++;
- pTmp = p = p+1;
- len = strlen(p);//用来保存最后一个字符串的长度
- }
- }
- else
- {
- //拷贝最后一个分割的字符串包括\0
- strncpy(buf[tmpcount],pTmp,len+1);
- break;
- }
- } while (*p!='\0');
- *count=tmpcount+1;
- return 0;
- }
- int main()
- {
- char *input="abcdefg,hjkln,sssss,kkk,hhh,j";
- char ctemp = ',';
- char **p = NULL;
- int ret;
- int n,i;
- p=(char**)malloc(10*sizeof(char*));
- if (p==NULL)
- {
- return;
- }
- for (i=0;i<10;i++)
- {
- p[i] = (char *)malloc(20*sizeof(char));
- }
- ret = splitStr(input,ctemp,p,&n);
- if (ret!=0)
- {
- printf("error\n");
- }
- for (i=0;i<n;i++)
- {
- printf("%s\n",*(p+i));
- }
- //释放内存
- system("pause");
- return 0;
- }
3)使用第三种内存模型 被调函数分配内存 通过return返回
[cpp] view plain copy print?
- #include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- #include <ctype.h>
- /*第三种内存模型 被调函数分配内存 通过return返回*/
- char** splitStr2(char *str,char c,int *count)
- {
- char* p = NULL,*pTmp = NULL;//两个辅助指针变量
- int tmpcount = 0,len,len2;
- char**buf;
- p = str;
- pTmp = str;
- //第一遍扫描 开辟第一维空间
- do
- {
- p = strchr(p,c);
- if (p!=NULL)
- {
- if (p-pTmp>0)
- {
- tmpcount++;
- pTmp = p = p+1;
- }
- }
- else
- {
- break;
- }
- } while (*p!='\0');
- *count=tmpcount+1;
- buf = (char**)malloc((tmpcount+1)*sizeof(char*));
- //printf("tmpcount:%d\n",tmpcount);
- if(buf==NULL)
- {
- return NULL;
- }
- //第二遍扫描 根据分割的字符串的长度,开辟第二维空间并拷贝字符串
- tmpcount = 0;
- p = str;
- pTmp = str;
- do
- {
- p = strchr(p,c);
- if (p!=NULL)
- {
- if (p-pTmp>0)
- {
- len = p-pTmp+1;
- buf[tmpcount] = (char*)malloc(len*sizeof(char));
- if (buf[tmpcount] == NULL)
- {
- free(buf);
- return ;
- }
- strncpy(buf[tmpcount],pTmp,p-pTmp);
- buf[tmpcount][p-pTmp] = '\0';
- tmpcount++;
- pTmp = p = p+1;
- len2 = strlen(p);//用来保存最后一个字符串的长度
- }
- }
- else
- {
- //printf("tmpcount:%d\n",tmpcount);
- //printf("len2:%d\n",len2);
- buf[tmpcount] = (char*)malloc((len2+1)*sizeof(char));
- if (buf[tmpcount] == NULL)
- {
- free(buf);
- return ;
- }
- //拷贝最后一个分割的字符串包括\0
- strncpy(buf[tmpcount],pTmp,len2+1);
- break;
- }
- } while (*p!='\0');
- return buf;
- }
- int main()
- {
- char *input="abcdefg,hjkln,sssss,kkk,hhh,j";
- char ctemp = ',';
- char **p = NULL;
- int ret;
- int n,i;
- p = splitStr2(input,ctemp,&n);
- printf("n:%d\n",n);
- for (i=0;i<n;i++)
- {
- printf("%s\n",*(p+i));
- }
- //释放内存空间
- system("pause");
- return 0;
- }
4)使用第三种内存模型 被调函数分配内存 通过函数参数返回
[cpp] view plain copy print?
- <pre name="code" class="cpp">#include<stdio.h>
- #include<stdlib.h>
- #include <string.h>
- #include <ctype.h>
- /*第三种内存模型 被调函数分配内存 通过tmpbuf返回*/
- int splitStr3(char *str,char c,char*** tmpbuf,int *count)
- {
- char* p = NULL,*pTmp = NULL;//两个辅助指针变量
- int tmpcount = 0,len,len2;
- char**buf;
- p = str;
- pTmp = str;
- //第一遍扫描 开辟第一维空间
- do
- {
- p = strchr(p,c);
- if (p!=NULL)
- {
- if (p-pTmp>0)
- {
- tmpcount++;
- pTmp = p = p+1;
- }
- }
- else
- {
- break;
- }
- } while (*p!='\0');
- *count=tmpcount+1;
- buf = (char**)malloc((tmpcount+1)*sizeof(char*));
- //printf("tmpcount:%d\n",tmpcount);
- if(buf==NULL)
- {
- return -1;
- }
- //第二遍扫描 根据分割的字符串的长度,开辟第二维空间并拷贝字符串
- tmpcount = 0;
- p = str;
- pTmp = str;
- do
- {
- p = strchr(p,c);
- if (p!=NULL)
- {
- if (p-pTmp>0)
- {
- len = p-pTmp+1;
- buf[tmpcount] = (char*)malloc(len*sizeof(char));
- if (buf[tmpcount] == NULL)
- {
- free(buf);
- return -2;
- }
- strncpy(buf[tmpcount],pTmp,p-pTmp);
- buf[tmpcount][p-pTmp] = '\0';
- tmpcount++;
- pTmp = p = p+1;
- len2 = strlen(p);//用来保存最后一个字符串的长度
- }
- }
- else
- {
- //printf("tmpcount:%d\n",tmpcount);
- //printf("len2:%d\n",len2);
- buf[tmpcount] = (char*)malloc((len2+1)*sizeof(char));
- if (buf[tmpcount] == NULL)
- {
- free(buf);
- return -2;
- }
- //拷贝最后一个分割的字符串包括\0
- strncpy(buf[tmpcount],pTmp,len2+1);
- break;
- }
- } while (*p!='\0');
- *tmpbuf = buf;
- return 0;
- }
- //释放内存空间
- void freeMem(char**p,int count)
- {
- int i =0;
- if (p == NULL)
- {
- return ;
- }
- for (i=0;i<count;i++)
- {
- if (p[i]!=NULL)
- {
- free(p[i]);
- }
- }
- if (p!=NULL)
- {
- free(p);
- }
- }
- int main()
- {
- char *input="abcdefg,hjkln,sssss,kkk,hhh,j";
- char ctemp = ',';
- char **p = NULL;
- int ret;
- int n,i;
- ret = splitStr3(input,ctemp,&p,&n);
- if (ret!= 0)
- {
- printf("error");
- return -1;
- }
- //printf("n:%d\n",n);
- for (i=0;i<n;i++)
- {
- printf("%s\n",*(p+i));
- }
- //释放内存
- freeMem(p,n);
- system("pause");
- return 0;
- }
阅读全文
0 0
- 二级指针的使用方法
- 二级指针使用方法
- 二级指针的妙用
- 二级指针的使用
- 二级指针的举例
- 二级指针的用法
- 二级指针的用法
- 二级指针的疑惑
- 二级指针的理解
- 二级指针的用法
- 二级指针的使用
- 二级指针的使用
- 二级指针的应用
- 二级指针的使用
- 二级指针的用法
- 二级指针的透析
- 二级指针的理解
- 指针的使用二级指针
- 0720Link
- MATLAB LINUX C相关资料收集
- hdu
- Ubuntu14配置tftp服务器
- 2017.7.20html5 output
- 二级指针的使用方法
- 利用协处理器endpoint实现批量删除功能
- Centos6.5+Ambari的HDP搭建
- 楼梯上楼问题
- Node.js安装express框架出现的问题级解决
- 数码管
- HDU 2602 Bone Collector(01背包)
- jquery 追加
- Angular(1)