C++ 常量char*和char const*-它们是一样的吗?

C++ 常量char*和char const*-它们是一样的吗?,c++,pointers,constants,C++,Pointers,Constants,根据我的理解,const修饰符应该从右向左读取。由此,我得到: const char* 是一个指针,其字符元素不能修改,但指针本身可以修改,并且 char const* 是指向mutable字符的常量指针 但是对于以下代码,我得到了以下错误: const char* x = new char[20]; x = new char[30]; //this works, as expected x[0] = 'a'; //gives an error as expected

根据我的理解,
const
修饰符应该从右向左读取。由此,我得到:

const char*
是一个指针,其字符元素不能修改,但指针本身可以修改,并且

char const*
是指向
mutable
字符的常量指针

但是对于以下代码,我得到了以下错误:

const char* x = new char[20];
x = new char[30];   //this works, as expected
x[0] = 'a';         //gives an error as expected

char const* y = new char[20];
y = new char[20];   //this works, although the pointer should be const (right?)
y[0] = 'a';         //this doesn't although I expect it to work

所以。。。是哪一个?我的理解或我的编译器(VS2005)是错误的吗?

它可以工作,因为两者都是相同的。也许你对此感到困惑

const char*  // both are same
char const*


[记住这一点,这里有一条简单的规则,“*”首先影响整个LHS]

这是因为该规则是:

规则:
const
绑定左侧,除非左侧没有任何内容,否则它将绑定右侧:)

因此,请将其视为:

(const --->> char)*
(char <<--- const)*
(常量-->>字符)*

(char
实际上,根据标准,
const
直接在元素的左侧修改元素。在声明开头使用
const
只是一种方便的心理捷径。因此以下两个语句是等效的:

char const * pointerToConstantContent1;
const char * pointerToConstantContent2;
为确保指针本身未被修改,
const
应置于星号之后:

char * const constantPointerToMutableContent;
要保护指针及其指向的内容,请使用两个常量

char const * const constantPointerToConstantContent;

我个人一直采用在我不想修改的部分后加常量,这样即使指针是我希望保持常量的部分,我也能保持一致性。

以下是我一直试图解释的方法:

char*p

     |_____ start from the asterisk. The above declaration is read as: "content of `p` is a `char`".
           |_____ again start from the asterisk. "content of `p` is a constant `char`".
char*const p

     |_____ again start from the asterisk. "content of constant (since we have the `const` 
            modifier in the front) `p` is a `char`".
char const*p

     |_____ start from the asterisk. The above declaration is read as: "content of `p` is a `char`".
           |_____ again start from the asterisk. "content of `p` is a constant `char`".
希望有帮助!

(来自)

关于
const
,有一个非常好的经验法则:

从右到左阅读声明。

(参见Vandevoorde/Josutiss“C++模板:完整指南”)

例如:

int const x; // x is a constant int
const int x; // x is an int which is const

// easy. the rule becomes really useful in the following:
int const * const p; // p is const-pointer to const-int
int const &p;        // p is a reference to const-int
int * const * p;     // p is a pointer to const-pointer to int.
自从我遵循这条经验法则以来,我再也没有曲解过这样的声明


(:sisab retcarahc rep a no ton,sisab nekot rep a no tfel ot thgir naem I HGUHT:tidE)在这两种情况下,您都指向一个常量字符

const char * x  //(1) a variable pointer to a constant char
char const * x  //(2) a variable pointer to a constant char
char * const x  //(3) a constant pointer to a variable char
char const * const x //(4) a constant pointer to a constant char
char const * const * x //(5) a variable pointer to a constant pointer to a constant char
char const * const * const x //(6) can you guess this one?

默认情况下,
const
应用于中间在左边的内容,但如果前面没有任何内容,则它可以应用于中间在右边的内容,如(1).

好的,我现在拿到了。非常感谢。
指向char*
的不可变指针。它是指向char
而不是char*的不可变指针。如果有疑问,请始终使用“…其char元素可以修改,但指针本身可以,并且…”-我想你是想对其中一个“can”说“can”s、 但我不知道你有多困惑,所以我不知道该纠正哪一个:p请检查此网站:www.cdecl.orgCompiler从未出错;)“便捷速记”是一个有趣的描述,它不短,也不遵循正常规则。标准并不关心你使用的顺序。第7.1.6节只是显示了两个地方,并说只在一个地方使用。@beetstra我同意。我把它改成了“便捷的心理捷径”更清楚一点。是的,正确的运算符是
-->
,它只对值进行操作。试试
int i=8;std::cout>1)+1这太棒了,谢谢!多年来,我一直在想一些事情,比如
const char*const
,多亏了你,我现在明白了。最后一个:常量变量指针,指向常量指针,指向常量字符。如果“常量变量指针”指的是“常量指针”,那么你就搞定了,兄弟!在(5)中,这是一个变量指针,指向一个常量指针,指向一个常量字符,因为在标识符“x”之前的最后一个星号右边没有“const”。但在(6)中,它变成了一个常量指针,其余的指针保持不变。