C提高_day03_两个辅助指针变量挖字符串(强化4)
2015-11-30 21:30
295 查看
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <string.h> #include <stdio.h> //两个辅助指针变量挖字符串, 的第三种内存模型 //指针做函数参数 void FreeMem(char **myp,int count) //释放内存函数 { int i=0; if(myp == NULL) { return; } for(i=0;i<count;i++) { if(myp[i] != NULL) { free(myp[i]); } } if(myp != NULL) { free(myp); } } int spitString4(char *buf1,char c,char ***myp3,int *count) //**pp二级指针做输入 { int ret =0; char *p=NULL, *pTmp = NULL; int tmpcount = 0; int len; char **myp=NULL; //1 p和ptmp初始化 p = buf1; pTmp = buf1; //第一遍求出count do { //2 检索符合条件的位置 p后移 形成差值 挖字符串 p = strchr(p, c); if (p != NULL) { if (p-pTmp > 0) { tmpcount ++; //3重新 让p和ptmp达到下一次检索的条件 pTmp = p = p + 1; } } else { break; } } while (*p!='\0'); *count = tmpcount; //根据多少行精确分配内存 myp=(char **)malloc(tmpcount * sizeof(char *)); if(myp==NULL) { ret=-1; printf("func spitSpring4() err :%d malloc(tmpcount * sizeof(char *))",ret); goto END; //return -1; } ///////////////////////////////////////////////////////// tmpcount=0; //1 p和ptmp初始化 p = buf1; pTmp = buf1; do { //2 检索符合条件的位置 p后移 形成差值 挖字符串 p = strchr(p, c); if (p != NULL) { if (p-pTmp > 0) { len=p-pTmp+1; myp[tmpcount]=(char *)malloc(len * sizeof(char)); if(myp==NULL) { //return -1; ret=-1; printf("func spitSpring4() err :%d malloc(tmpcount * sizeof(char *))",ret); goto END; } strncpy(myp[tmpcount],pTmp,p-pTmp); myp[tmpcount][p-pTmp]='\0'; tmpcount ++; //3重新 让p和ptmp达到下一次检索的条件 pTmp = p = p + 1; } } else { break; } } while (*p!='\0'); END: if(ret != 0) //失败 { FreeMem(myp,*count); } else { *myp3 = myp; //成功 } return ret; } int main() { int i; int ret=0 ; char *p1="abcdef,aaa,eeeee,ffffff,a3a3a3,"; char tmp=','; char **p=NULL; int nCount; ret=spitString4(p1,tmp,&p,&nCount); if(ret!=0) { printf("fun spiltString() err:%d \n",ret); return ret; } for(i=0;i<nCount;i++) { printf("%s \n",p[i]); } for(i=0;i<nCount;i++) { free(p[i]); } free(p); printf("%d \n",nCount); printf("hello...\n"); system("pause"); }
相关文章推荐
- Dijkstra+计算几何 POJ 2502 Subway
- 编译GDAL支持OpenCL使用GPU加速
- 仿QQ控件,朋友圈
- qt_简易记事本_1
- Eclipse文档注释生成doc方法
- 编译GDAL支持OpenCL使用GPU加速
- Git使用教程
- k-近邻算法实现分类器
- javascript-reduce 方法 (Array)
- 加解密杂烩程序开发(二)
- WebKit之Node的继承关系图
- 信息安全系统设计基础第十二周总结
- Oracle Listener 动态注册 与 静态注册
- B. Mike and Fun---cf548B(暴力求解)
- 最新一课 老师指点用Listview适配器
- Hadoop2.6.0学习笔记(四)TextInputFormat及RecordReader解析
- 【Android】RecyclerView的OnItemClickListener点击事件处理
- 谷歌原生下拉刷新SwipeRefreshLayout
- Android代码混淆常见配置
- N皇后问题