h*****g 发帖数: 944 | 1 谢谢大家指点,compile有warning, 执行起来segmentation fault
#include
using namespace std;
void reverse (char *str);
int main(){
char * str = "hello";
reverse(str);
cout<
}
void reverse(char *str){
char * end = str;
char tmp;
if(str){
while(*end){
++end;
}
--end;
while(str
tmp = *str;
*str++=*end;
*end--=tmp;
}//end while
}
} |
c******t 发帖数: 1500 | 2 你这样定义的str是字符串常量,不能被修改
char str[] = "hello";
才可以 |
c******t 发帖数: 1500 | 3 我改了一点你的程序,刚在VS 2008下编译通过了
#include
using namespace std;
void reverse (char *str);
int main(){
char str[] = "hello";
reverse(str);
cout<
}
void reverse(char *str){
char* begin = str;
char* end = str;
char tmp;
if(str)
{
while(*end)
++end;
--end;
while(begin
{
tmp = *begin;
*begin = *end;
*end=tmp;
++begin;
--end;
}
}
} |
c***2 发帖数: 838 | 4 your program seems fine. what compiler do you use?
I tried it with visual c++ 6.0:
c:\temp>cpp rstr.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
rstr.cpp
rstr.cpp(10) : warning C4508: 'main' : function should return a value; 'void
' return type assumed
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:rstr.exe
rstr.obj
Run [rstr]:
olleh |