由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ 初级再初级问题
相关主题
这个C++程序为什么不能运行为什么foo1可以而foo2不行?
What is wrong with the code?C的argc问题
菜鸟求教,一个c++的困惑const 指针类型转换
A aimple C++ question问个fork cow的问题
大侠们救命, C++ operator new 问题Use Visual .NET for C++ programming
请问一个exception题目三个C syntax 弱问题
奇怪的问题:关于一个简单的malloc()小程序 (转载)a question on C++ string
char ** pt1和 char * pt2[] 的区别在哪?关于c++的constructor的面试题
相关话题的讨论汇总
话题: text话题: char话题: total话题: int话题: tmp
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
关于c++的constructor的面试题大侠们救命, C++ operator new 问题
请问C++中局部未使用的变量在优化的时候会去掉么?请问一个exception题目
C++菜问: 怎么这样也可以?奇怪的问题:关于一个简单的malloc()小程序 (转载)
A helloworld OpenMP question? char ** pt1和 char * pt2[] 的区别在哪?
这个C++程序为什么不能运行为什么foo1可以而foo2不行?
What is wrong with the code?C的argc问题
菜鸟求教,一个c++的困惑const 指针类型转换
A aimple C++ question问个fork cow的问题
相关话题的讨论汇总
话题: text话题: char话题: total话题: int话题: tmp