读书人

字符串反转解决方案

发布时间: 2013-09-17 13:35:59 作者: rapoo

字符串反转
char * strRev_new(char *s)
{
char* ret = s;
char temp, *end = s + strlen(s) - 1;
while( end > s)
{
temp = *s;
*s = *end; // 为什么这句会出错
*end = temp;
--end;
++s;
}
return ret;
}

为什么这段代码那句总出错呢?

[解决办法]
char *str="hello,world!"指向的是常量字符串,在内存的常量区,不能被改变,而char str[]="hello,world!"存在内存的栈区,局部变量,是可以被改变的

读书人网 >C语言

热点推荐