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
|