由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - const char *p, is it ok to change p[1] ?
相关主题
数组指针的问题一个c语言的问题
a simple C++ question问一道brainbench上的问题
最新某公司onsite面试题 (转载)谁给解释一下这个c question
why int** cannot convert to const int** ?问一个 char * 和 char [] 的问题
strlen怎么实现的c++ 弱问题:static const char* const 这两个const 分别是什么意思?
请问以下代码有什么错误?what's wrong with this C++ code?
C 语言,初学者,简单问题[合集] 一个指针的小问题
请问const myClass &src 和myClass const &src有什么区别?pointer to function
相关话题的讨论汇总
话题: const话题: char话题: ok话题: change话题: pointer
进入Programming版参与讨论
1 (共1页)
r*******y
发帖数: 1081
1
for example
char a[] = "hello";
const char * p = a;
p[1] = '3'; //is this ok?
I know it is not ok for this: *p = '3'
since p point to a const char.
But it is not saying p+1 is pointing to a const char.
so p[1] ='3' would have been ok.
But I find that it is not ok to do this: p[1] = '3' after I compile it
Any comments? seems a stupid question. thanks
X****r
发帖数: 3557
2
type of p is const char *, so type of p+1 is also const char *,
thus type of p[1], which is *(p+1), is const char, and you can't
assign to it.

【在 r*******y 的大作中提到】
: for example
: char a[] = "hello";
: const char * p = a;
: p[1] = '3'; //is this ok?
: I know it is not ok for this: *p = '3'
: since p point to a const char.
: But it is not saying p+1 is pointing to a const char.
: so p[1] ='3' would have been ok.
: But I find that it is not ok to do this: p[1] = '3' after I compile it
: Any comments? seems a stupid question. thanks

i**********e
发帖数: 1145
3
Not a stupid question at all. We all learn from asking questions.
p[1] is converted to *(p+1) in compiler.
p is declared as a character pointer to a constant string. Therefore,
although the pointer can be incremented/decremented, it is illegal to use
the pointer to change the data it is pointing to.
r*******y
发帖数: 1081
4
that is my question: type of p is const char *, why type of p+1 is
also const char * ?
thanks

【在 X****r 的大作中提到】
: type of p is const char *, so type of p+1 is also const char *,
: thus type of p[1], which is *(p+1), is const char, and you can't
: assign to it.

a***y
发帖数: 2803
5
随便找本c++的教课书,就知道了.
你這個是试图钻compiler的漏洞但是没钻到点子上.如果要钻,应该把
p[1] = '3' 改为
a[1] = '3'

【在 r*******y 的大作中提到】
: for example
: char a[] = "hello";
: const char * p = a;
: p[1] = '3'; //is this ok?
: I know it is not ok for this: *p = '3'
: since p point to a const char.
: But it is not saying p+1 is pointing to a const char.
: so p[1] ='3' would have been ok.
: But I find that it is not ok to do this: p[1] = '3' after I compile it
: Any comments? seems a stupid question. thanks

i**********e
发帖数: 1145
6
http://www.parashift.com/c++-faq-lite/const-correctness.html#fa
Fred const* p means "p points to a constant Fred": the Fred object can't be
changed via p.
In your case, you are still trying to change the data p is pointing to, even
though you use p+1. What is the point of const-correctness if you can
increment the pointer and changing the data that is declared as const?

【在 r*******y 的大作中提到】
: that is my question: type of p is const char *, why type of p+1 is
: also const char * ?
: thanks

a***y
发帖数: 2803
7
p指针指向的那個以'\0'结尾的string是不能改变的.所以,如果strlen(string)长度是5
,那么p[0],p[1],p[2],p[3],p[4]都是不可改变的.

【在 r*******y 的大作中提到】
: that is my question: type of p is const char *, why type of p+1 is
: also const char * ?
: thanks

X****r
发帖数: 3557
8
because it's turtle's ass...
joke aside, think about it yourself. what type you think p+1 should be?

【在 r*******y 的大作中提到】
: that is my question: type of p is const char *, why type of p+1 is
: also const char * ?
: thanks

j*******d
发帖数: 8834
9
depending on your machine/arch/compiler
"hello" could be in text segment, or the .rodata, or even data segment.
如果是前二,大多数OS会直接seg fault你
如果是data segment,应该没问题。

【在 r*******y 的大作中提到】
: for example
: char a[] = "hello";
: const char * p = a;
: p[1] = '3'; //is this ok?
: I know it is not ok for this: *p = '3'
: since p point to a const char.
: But it is not saying p+1 is pointing to a const char.
: so p[1] ='3' would have been ok.
: But I find that it is not ok to do this: p[1] = '3' after I compile it
: Any comments? seems a stupid question. thanks

1 (共1页)
进入Programming版参与讨论
相关主题
pointer to functionstrlen怎么实现的
问个c++在不同函数里分配内存和释放内存的弱问题请问以下代码有什么错误?
我写的这个C++错在哪里?C 语言,初学者,简单问题
请教boost::any compile错误。请问const myClass &src 和myClass const &src有什么区别?
数组指针的问题一个c语言的问题
a simple C++ question问一道brainbench上的问题
最新某公司onsite面试题 (转载)谁给解释一下这个c question
why int** cannot convert to const int** ?问一个 char * 和 char [] 的问题
相关话题的讨论汇总
话题: const话题: char话题: ok话题: change话题: pointer