首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

字符串反转解决方案

2013-09-17 
字符串反转char * strRev_new(char *s){char* ret schar temp, *end s + strlen(s) - 1while( end

字符串反转
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!"存在内存的栈区,局部变量,是可以被改变的

热点排行