char *strip_postfix(char *str1, char *str2)
{
int i;
for(i=0; str2[i] != '
char *strip_postfix(char *str1, char *str2)
{
int i;
for(i=0; str2[i] != '\0'; i++)
{
if(str1[i] != str2[i])
{
str1[i] = '\0';
break;
}
}
return str3;
}
This code, is giving segmentation fault error at line str1[i] = '\0' during run time..... I think there is some memory allocation issue,, as while I create a new variable and copy the contents there, and then return that new variable, everything works fine.... Please let me know what's the issue in this.
此代码在运行时在行str1 [i] ='\ 0'给出分段错误错误.....我认为存在一些内存分配问题,因为当我创建一个新变量并将内容复制到那里时,然后返回那个新变量,一切正常....请告诉我这是什么问题。
2 个解决方案
#1
4
What happens if str1 is smaller than str2?
如果str1小于str2会发生什么?
You also have:
你还有:
return str3;
Which makes me think this code will not compile in its current form...
这让我觉得这段代码不会以当前形式编译......
#2
0
Your code should like
你的代码应该是
int str1len = strlen(str1);
for(i=0; str2[i] != '\0'; i++)
{
if(str1[i] != str2[i] || i >= str1len)
{
str1[i] = '\0';
break;
}
}
return str1;
Thanks
'; i++)
{
if(str1[i] != str2[i])
{
str1[i] = '
char *strip_postfix(char *str1, char *str2)
{
int i;
for(i=0; str2[i] != '\0'; i++)
{
if(str1[i] != str2[i])
{
str1[i] = '\0';
break;
}
}
return str3;
}
This code, is giving segmentation fault error at line str1[i] = '\0' during run time..... I think there is some memory allocation issue,, as while I create a new variable and copy the contents there, and then return that new variable, everything works fine.... Please let me know what's the issue in this.
此代码在运行时在行str1 [i] ='\ 0'给出分段错误错误.....我认为存在一些内存分配问题,因为当我创建一个新变量并将内容复制到那里时,然后返回那个新变量,一切正常....请告诉我这是什么问题。
2 个解决方案
#1
4
What happens if str1 is smaller than str2?
如果str1小于str2会发生什么?
You also have:
你还有:
return str3;
Which makes me think this code will not compile in its current form...
这让我觉得这段代码不会以当前形式编译......
#2
0
Your code should like
你的代码应该是
int str1len = strlen(str1);
for(i=0; str2[i] != '\0'; i++)
{
if(str1[i] != str2[i] || i >= str1len)
{
str1[i] = '\0';
break;
}
}
return str1;
Thanks
';
break;
}
}
return str3;
}
char *strip_postfix(char *str1, char *str2)
{