您的位置:首页 > 编程语言 > C语言/C++

【C语言提高30】二级指针强化训练

2015-12-06 23:39 543 查看
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>

//主调函数分配内存
int spitString2(char*buf1, char c, char**myp/*in*/, int*count)
{
//strcpy(buf2[0], "aaaaa");
//strcpy(buf2[1], "bbbbbb");
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';  //把第一行数据变成 C风格字符串
tmpcount++;
//3重新 让p和ptmp达到下一次检索的条件
pTmp = p = p + 1;
}
}
else
{
break;
}
} while (*p != '\0');

*count = tmpcount;
return 0;
}

void main81()
{
int ret = 0, i = 0;
char *p1 = "abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";
char cTem = ',';
int nCount;

char **p = NULL;
p = (char**)malloc(sizeof(char*) * 10);// 相当于 char*array[10];
if (p == NULL)
{
return;
}
for (i = 0; i < 10; i++)
{
p[i] = (char*)malloc(sizeof(char)*30);
}

ret = spitString2(p1, cTem, p, &nCount);
if (ret != 0)
{
printf("fucn spitString() err: %d \n", ret);
return;
}

for (i = 0; i<nCount; i++)
{
printf("%s \n", p);
}

for (i = 0; i < 10; i++)
{
free(p[i]);
}
free(p);

printf("hello...\n");
system("pause");
return;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: