#include <stdlib.h> #include <string.h> #include <stdio.h> //两个辅助指针变量挖字符串, 的第三种内存模型 int spitString2(const char * buf1,char c,char **myp/*in*/,int *count) //**pp二级指针做输入 { char *p=NULL, *pTmp = NULL; int tmpcount = 0; //1 p和ptmp初始化 p = buf1; pTmp = buf1; do { //2 检索符合条件的位置 p后移 形成差值 挖字符串 p = strchr(p, c); if (p != NULL) { if (p-pTmp > 0) { strncpy(myp[tmpcount], pTmp, p-pTmp); myp[tmpcount][p-pTmp]=‘\0‘; tmpcount ++; //3重新 让p和ptmp达到下一次检索的条件 pTmp = p = p + 1; } } else { break; } } while (*p!=‘\0‘); *count = tmpcount; return 0; } void main() { int ret=0,i; char *p1="abcdef,aaa,eeeee,ffffff,a3a3a3,"; char tmp=‘,‘; char **p=NULL; int nCount; //可单独写个函数 //char buf[10][30] p=(char **)malloc(10*sizeof(char *)); //char *array[10] if(p==NULL) { return; } for(i=0;i<10;i++) { p[i]=(char *)malloc(30 *sizeof(char)); return ret; } ret=spitString2(p1,tmp,p,&nCount); if(ret !=0) { printf("fun spiltString() err:%d \n", ret); } for(i=0;i<nCount;i++) { printf("%s \n",p[i]); } for(i=0;i<10;i++) { free(p[i]); } free(p); printf("%d \n",nCount); printf("hello...\n"); system("pause"); }
原文:http://www.cnblogs.com/zhesun/p/4996228.html