C 在找到字符时剪切字符串
我编写了一个函数,如果找到一个“o”,它会将字符串“hello world”剪切为“hell” 我总是遇到一个分割错误。我不知道哪里会出错。 有人能帮忙吗? 先谢谢你C 在找到字符时剪切字符串,c,arrays,string,function,C,Arrays,String,Function,我编写了一个函数,如果找到一个“o”,它会将字符串“hello world”剪切为“hell” 我总是遇到一个分割错误。我不知道哪里会出错。 有人能帮忙吗? 先谢谢你 #include <stdio.h> #include <stdlib.h> #include <string.h> char* cutString(char* str, char del){ char *newstring =(char*) str; malloc(size
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* cutString(char* str, char del){
char *newstring =(char*) str;
malloc(sizeof(char)*strlen(str));
int i= 0;
for(; newstring[i]!='\0'&&newstring[i]!=del;i++);
if(i==strlen(newstring))
printf("not found");
else
newstring[i]='\0';
return newstring;
}
int main(){
cutString("Hello World",'o');
return 0;
}
#包括
#包括
#包括
char*cutString(char*str,char-del){
char*newstring=(char*)str;
malloc(sizeof(char)*strlen(str));
int i=0;
for(;newstring[i]!='\0'&&newstring[i]!=del;i++);
if(i==strlen(newstring))
printf(“未找到”);
其他的
新闻字符串[i]='\0';
返回新闻字符串;
}
int main(){
cutString(“你好,世界”,“o”);
返回0;
}
您的代码有两个主要问题:
char*newstring=(char*)str
使newstring
指向旧的str
。由于您传递了一个文本字符串(只读),您将有未定义的行为试图修改它malloc(sizeof(char)*strlen(str))代码>是内存泄漏。不为终结者分配空间
崩溃可能是因为第一点,当您试图修改只读字符串文字时。代码有两个主要问题:
newstring[i]='\0';
char*newstring=(char*)str
使newstring
指向旧的str
。由于您传递了一个文本字符串(只读),您将有未定义的行为试图修改它malloc(sizeof(char)*strlen(str))代码>是内存泄漏。不为终结者分配空间
newstring[i]='\0';
这行无效。修改字符串文字是未定义的行为。我建议你看看这个:
更好的解决方案是使用数组而不是指针
这行无效。修改字符串文字是未定义的行为。我建议你看看这个:
更好的解决方案是使用数组而不是指针代码中存在许多问题。主要问题是您没有将返回值从
malloc
分配给newstring
。除此之外,还需要malloc
一个额外的字节来终止字符串
此外,循环必须将字符从str
复制到newstring
在main
中,必须将函数的返回值分配给char指针变量,以获得新字符串
比如:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* cutString(char* str, char del){
char *newstring = malloc(strlen(str) + 1); // malloc into newstring
int i= 0;
while (newstring[i]!='\0' && str[i] != del) // Stop when a) no more chars in str or b) "del" is found
{
newstring[i] = str[i]; // Copy character from str to newstring
++i;
}
newstring[i]='\0'; // Terminate the string
return newstring;
}
int main(){
char* newstring = cutString("Hello World",'o'); // Save the returned value
printf("%s\", newstring);
free(newstring);
return 0;
}
#包括
#包括
#包括
char*cutString(char*str,char-del){
char*newstring=malloc(strlen(str)+1);//malloc-into-newstring
int i=0;
while(newstring[i]!='\0'&&str[i]!=del)//当a)str或b)“del”中没有更多字符时停止
{
newstring[i]=str[i];//将字符从str复制到newstring
++一,;
}
newstring[i]='\0';//终止字符串
返回新闻字符串;
}
int main(){
char*newstring=cutString(“Hello World”,“o”);//保存返回值
printf(“%s\”,新闻字符串);
免费(新闻字符串);
返回0;
}
您的代码中存在许多问题。主要问题是您没有将返回值从malloc
分配给newstring
。除此之外,还需要malloc
一个额外的字节来终止字符串
此外,循环必须将字符从str
复制到newstring
在main
中,必须将函数的返回值分配给char指针变量,以获得新字符串
比如:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* cutString(char* str, char del){
char *newstring = malloc(strlen(str) + 1); // malloc into newstring
int i= 0;
while (newstring[i]!='\0' && str[i] != del) // Stop when a) no more chars in str or b) "del" is found
{
newstring[i] = str[i]; // Copy character from str to newstring
++i;
}
newstring[i]='\0'; // Terminate the string
return newstring;
}
int main(){
char* newstring = cutString("Hello World",'o'); // Save the returned value
printf("%s\", newstring);
free(newstring);
return 0;
}
#包括
#包括
#包括
char*cutString(char*str,char-del){
char*newstring=malloc(strlen(str)+1);//malloc-into-newstring
int i=0;
while(newstring[i]!='\0'&&str[i]!=del)//当a)str或b)“del”中没有更多字符时停止
{
newstring[i]=str[i];//将字符从str复制到newstring
++一,;
}
newstring[i]='\0';//终止字符串
返回新闻字符串;
}
int main(){
char*newstring=cutString(“Hello World”,“o”);//保存返回值
printf(“%s\”,新闻字符串);
免费(新闻字符串);
返回0;
}
malloc(sizeof(char)*strlen(str))代码>?您需要了解返回值是什么以及它们的含义。您正在使用该行代码创建一个内存块,并将其丢弃,因为您没有保存返回值。malloc(sizeof(char)*strlen(str))代码>?您需要了解返回值是什么以及它们的含义。您正在使用该行代码创建一个内存块,并将其丢弃,因为您没有保存返回值代码>@momonosuke这可能是一个好的开始,但还需要一些其他工作,因为内存将不包含源字符串。如前所述,如果复制整个源字符串,则没有足够的空间容纳终止符代码>@momonosuke这可能是一个好的开始,但还需要一些其他工作,因为内存将不包含源字符串。如前所述,如果复制整个源字符串,则没有足够的空间容纳终止符。问题是我必须为该字符串分配新内存。这是我的作业,我读了这篇文章,我明白了为什么我的方法不正确。问题是我必须为字符串分配新的内存。这是我的任务,我读了这篇文章,我明白为什么我的方法是错误的