#include<stdio.h>
#include<string.h>
void reverseMe(char *,int,int);
int main()
{
char str[]="cycle";
int len=strlen(str);
reverseMe(str,0,len-1);
return 0;
}
void reverseMe(char *x,int begin,int end)
{
char c;
c=*(x+begin);
*(x+begin)=*(x+end);
*(x+end)=c;
reverseMe(x,++begin,--end);
}
为什么会出现段错误? 我犯了什么错误?
最佳答案
嗯?
您永远不会检查字符串的限制,reverseMe()
会无限递归。当然会给你放烟花。
你应该有类似的东西
if(begin < end)
{
const char c = x[begin];
x[begin] = x[end];
x[end] = c;
reverseMe(x, begin + 1, end - 1);
}
在reverseme()内
。另请注意,在许多情况下,数组索引比指针算术更清晰。
关于c - 段错误错误: While reversing a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24633719/