• C提高_day03_两个辅助指针变量挖字符串(强化3)


    #define  _CRT_SECURE_NO_WARNINGS 
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    //两个辅助指针变量挖字符串, 的第三种内存模型
    
    //指针做函数参数
    int spitString4(char *buf1,char c,char ***myp3,int *count)    //**pp二级指针做输入
    {
        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!='');
    
        *count = tmpcount;
    
        //根据多少行精确分配内存
        myp=(char **)malloc(tmpcount * sizeof(char *));
        if(myp==NULL)
        {
            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;
                    }
                    strncpy(myp[tmpcount],pTmp,p-pTmp);
                    myp[tmpcount][p-pTmp]='';
                    tmpcount ++;
                    //3重新 让p和ptmp达到下一次检索的条件
                    pTmp = p = p + 1;
                }
            }
            else
            {
                break;
            }
        } while (*p!='');
    
    
        //*count = tmpcount;
    
        *myp3 = myp;   //myp3是二级指针地址的值
        return 0;
    
    }
    
    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 
    ",ret);
            return ret;
        }
    
        for(i=0;i<nCount;i++)
        {
            printf("%s 
    ",p[i]);
        }
    
        for(i=0;i<nCount;i++)
        {
            free(p[i]);
        }
        free(p);
    
        printf("%d 
    ",nCount);
        printf("hello...
    ");
        system("pause");
    
    }
    Stay hungry,Stay foolish
  • 相关阅读:
    JDK14的新特性:JFR,JMC和JFR事件流
    java深入理解浅拷贝和深拷贝
    jcmd:JDK14中的调试神器
    你真的了解java class name吗?
    JDK14中的java tools简介
    JDK 14的新特性:switch表达式
    JDK 14的新特性:文本块Text Blocks
    java中的内部类内部接口详解
    一文弄懂String的所有小秘密
    深入理解java的泛型
  • 原文地址:https://www.cnblogs.com/zhesun/p/5003826.html
Copyright © 2020-2023  润新知