|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
照视频打的代码 把一个字符串 char* p = "abcdef,acccd,eeee,aaaa,e3eeeee,sssss,"; 然后以逗号‘,’为分隔
组成一个个二维数组 比如abcdef是二维数组第一个元素,acccd是二维数组第二个元素
这个应该怎么写?我照视频打的 p2[count][p1 - p2] = '\0'; 报错 说是指针类型
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int getMen(char* from, char c, char myBuf[10][30], int* ncount)
{
if (from == NULL || ncount == NULL)
{
return -1;
}
char* myfrom = from;
int count = 0;
char* p1 = myfrom;
char* p2 = myfrom;
while (*myfrom)
{
p1 = strchar(p1, c);
if (p1 != NULL)
{
if (p1 - p2 > 0)
{
strncpy(p2[count], p1, p1 - p2);
p2[count][p1 - p2] = '\0'; //这里p1 - p2 报错
count++;
p1 = p2 = p1 + 1;
}
}
else
{
break;
}
}
*ncount = count;
return 0;
}
int main()
{
int ret = 0;
char* p = "abcdef,acccd,eeee,aaaa,e3eeeee,sssss,";
char c = ',';
int count = 0;
char buf2[10][30];
ret = getMen(p, c, buf2, &count);
if (ret != 0)
{
printf("fun:getMen() from == NULL || ncount == NULL err:%d\n", ret);
return ret;
}
system("pause");
return 0;
}
本帖最后由 jackz007 于 2020-3-4 13:41 编辑 #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "stdlib.h"
int main()
{
int count , e , k ;
char Text[] = {"abcdef,acccd,eeee,aaaa,e3eeeee,sssss,"} , buf[10][30] ;
for(k = 0 , count = 0 , e = 0 ; Text[k] ; k ++) {
if(Text[k] != ',') {
buf[count][e ++] = Text[k] ;
buf[count][e] = '\0' ;
} else {
count ++ ;
e = 0 ;
}
}
if(e) count ++ ;
for(k = 0 ; k < count ; k ++) printf("%s\n" , buf[k]) ;
system("pause") ;
}
编译运行实况: C:\Bin>g++ -o x x.c
C:\Bin>x
abcdef
acccd
eeee
aaaa
e3eeeee
sssss
请按任意键继续. . .
C:\Bin>
|
|