J*******g 发帖数: 381 | 1 小弟就是想reverse一个字符串,但是下面的程序在
text[i] = text[total-1-i];
这一句出错了,运行告诉bus error。 哪位大虾可以指点一下么? 多谢!
#include
#include
#include
using namespace std;
int main (int argc, char * const argv[]) {
char *text = "everyone is created equal, but some are more equal than
others.";
int total = strlen(text);
char tmp = 0;
for(int i=0; i
{
tmp = text[i];
text[i] = text[total-1-i];
text[total-1-i]= tmp; |
X****r 发帖数: 3557 | 2 char *text改为char text[]
【在 J*******g 的大作中提到】 : 小弟就是想reverse一个字符串,但是下面的程序在 : text[i] = text[total-1-i]; : 这一句出错了,运行告诉bus error。 哪位大虾可以指点一下么? 多谢! : #include : #include : #include : using namespace std; : : int main (int argc, char * const argv[]) { : char *text = "everyone is created equal, but some are more equal than
|
J*******g 发帖数: 381 | 3 可以请教下,为啥用char * c++ 就把字符串定死了吗?
【在 X****r 的大作中提到】 : char *text改为char text[]
|
X****r 发帖数: 3557 | 4 C++ 标准说:
The effect of attempting to modify a string literal is undefined.
【在 J*******g 的大作中提到】 : 可以请教下,为啥用char * c++ 就把字符串定死了吗?
|
J*******g 发帖数: 381 | 5 Thnx
【在 X****r 的大作中提到】 : C++ 标准说: : The effect of attempting to modify a string literal is undefined.
|
h*******e 发帖数: 155 | 6 我记得是. char * = "xxxx" . 指针指的的是字符串常量。 "xxxxx"是在常量空间生成了。你不能改变它的值。
【在 J*******g 的大作中提到】 : 小弟就是想reverse一个字符串,但是下面的程序在 : text[i] = text[total-1-i]; : 这一句出错了,运行告诉bus error。 哪位大虾可以指点一下么? 多谢! : #include : #include : #include : using namespace std; : : int main (int argc, char * const argv[]) { : char *text = "everyone is created equal, but some are more equal than
|