这是我的代码:
void reverseStr(char *str)
{
if (str == NULL) return;
int i=0, j=strlen(str) -1;
while(i<j)
{
char temp = str[j]; //i think this is the cause of the problem
str[j] = str[i];
str[i] = temp;
i++;
j--;
}
}
Run Code Online (Sandbox Code Playgroud)
所以这里是它的所在:
int main()
{
char *str = "Forest Gump";
reverseStr(str);
cout << str;
}
Run Code Online (Sandbox Code Playgroud)
这是我的错误:
/Applications/TextMate.app/Contents/SharedSupport/Bundles/C.tmbundle/Support/bin/bootstrap.sh:line 7:1931总线错误"$ 3".out
有什么想法吗?提前致谢.
Str指向固定字符串.你正在修改它.换句话说,您尝试更改文本文字.试试这个:
char *str = strdup("Forest Gump");
reverseStr(str);
cout << str;
free(str);
Run Code Online (Sandbox Code Playgroud)