我试图将其插入C中,我不知道为什么我的代码在做strcat时会崩溃
我试图将其插入C中,我不知道为什么我的代码在做strcat时会崩溃
char** split(const char* str, char delimiter)
{
int index=0;
int size=num_items(str,delimiter);
char split[size+1];
//单元格代表阵列中的单元格
char* cell=(char*)malloc(1);
for(int i=0; i<strlen(str); i++)
{
if(str[i]!=delimiter)
{
在添加下一个炭之前的返回大小的大小增加了每个未介绍的char
cell=realloc(cell,sizeof(cell)+8);
***the program get crashed after the strcat***
strcat(cell,(unsigned const char*)str[i]);
}else{
split[index]= (char) cell;
free(cell);
char* cell=(char*)malloc(0);
}
}
return split;
}
*** ***,在再次写入后,我对strcat也有同样的问题,可以向我解释我错误?***
char** split(const char* str, char delimiter)
{
int ch=0;
int word=0;
const char * zero="\0";
unsigned int size=num_items(str,delimiter);
//char* cell=calloc(1,sizeof(char));
char** split= calloc(size+1,sizeof(char*));
for(int i=0; i<strlen(str); i++) //i=colum
{
if(ch==0)
{ //strcat(split[word],zero);
memset(split[word],'\0',1);
ch++;
}
if(str[i]!=delimiter)
{
strcat(split[word],&str[i]);
ch++;
}else{
ch=0;
word++;
}
}
return split;
}
i am trying to implemet split in c , i doesnt know why the my code get crashed when he do strcat
char** split(const char* str, char delimiter)
{
int index=0;
int size=num_items(str,delimiter);
char split[size+1];
// cell represent the cell in the array to return
char* cell=(char*)malloc(1);
for(int i=0; i<strlen(str); i++)
{
if(str[i]!=delimiter)
{
Increases the size of the cell before adding to it the next char every unsinged char is 8bits
cell=realloc(cell,sizeof(cell)+8);
***the program get crashed after the strcat***
strcat(cell,(unsigned const char*)str[i]);
}else{
split[index]= (char) cell;
free(cell);
char* cell=(char*)malloc(0);
}
}
return split;
}
*** after writting again, i have the same problem with strcat, can somone explain to me my mistakes?***
char** split(const char* str, char delimiter)
{
int ch=0;
int word=0;
const char * zero="\0";
unsigned int size=num_items(str,delimiter);
//char* cell=calloc(1,sizeof(char));
char** split= calloc(size+1,sizeof(char*));
for(int i=0; i<strlen(str); i++) //i=colum
{
if(ch==0)
{ //strcat(split[word],zero);
memset(split[word],'\0',1);
ch++;
}
if(str[i]!=delimiter)
{
strcat(split[word],&str[i]);
ch++;
}else{
ch=0;
word++;
}
}
return split;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在动态分配内存的范围内
您正在使用魔术表达式
sizeof(cell)+8
(这是没有意义的),。因此,由于内存的范围不包含字符串,因此您不得使用函数
strcat
除此之外,在任何情况下,函数都不是有意义的,并且具有未定义的行为。例如,数组
split
是该函数的本地数组,该数组在此返回语句中
具有自动存储持续时间,该数组指定器被隐式转换为指针,转换为其类型
char *。但是,函数返回类型为
char **
。这是返回表达式的类型,函数的返回类型不兼容。此外,本地数组 split 退出功能后将不会活着。因此,无论如何,返回的指针都是无效的。
否则此陈述
没有意义。
您需要重新重写功能。
You are allocating dynamically an uninitialized extent of memory
using the magic expression
sizeof(cell)+8
(that does not make a sense).So as the extent of memory does not contain a string you may not use the function
strcat
likeApart from this the function in any case does not make a sense and has undefined behavior. For example the array
split
is a local array of the function with automatic storage durationIn this return statement
the array designator is implicitly converted to a pointer to its first element of the type
char *
. However the function return type ischar **
.That is the type of the returned expression and the return type of the function are not compatible. Moreover the local array
split
will not be alive after exiting the function. So in any case the returned pointer will be invalid.Or this statement
does not make a sense.
You need to rewrite the function anew.
PS已经实现了分裂的实现,您可以检查一下或将其用作片段。
这是项目: libxutils 。在
src/xstr.c
文件中检查xstrsplit()
函数。示例代码看起来像这样:
P.S. there is an already made implementation of split and you can check this out or use it as a snippet.
This is the project: libxutils. Check
xstrsplit()
function insrc/xstr.c
file.The example code is looking something like that: