简体   繁体   English

const char *和char const * - 它们是一样的吗?

[英]const char* and char const* - are they the same?

From my understanding, const modifiers should be read from right to left. 根据我的理解,应该从右到左阅读const修饰符。 From that, I get that: 从那以后,我明白了:

const char*

is a pointer whose char elements can't be modified, but the pointer itself can, and 是一个指针,其char元素不能被修改,但指针本身可以,和

char const*

is a constant pointer to mutable chars. 是一个指向mutable字符的常量指针。

But I get the following errors for the following code: 但是我得到以下代码的以下错误:

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

So... which one is it? 那么是哪一个呢? Is my understanding or my compiler(VS 2005) wrong? 我的理解还是我的编译器(VS 2005)错了?

Actually, according to the standard, const modifies the element directly to its left . 实际上,根据标准, const将元素直接修改为其左侧 The use of const at the beginning of a declaration is just a convenient mental shortcut. 在声明开头使用const只是一个方便的心理快捷方式。 So the following two statements are equivalent: 所以以下两个陈述是等价的:

char const * pointerToConstantContent1;
const char * pointerToConstantContent2;

In order to ensure the pointer itself is not modified, const should be placed after the asterisk: 为了确保指针本身不被修改, const应该放在星号后面:

char * const constantPointerToMutableContent;

To protect both the pointer and the content to which it points, use two consts. 要保护指针和指向的内容,请使用两个consts。

char const * const constantPointerToConstantContent;

I've personally adopted always putting the const after the portion I intend not to modify such that I maintain consistency even when the pointer is the part I wish to keep constant. 我个人采用总是将const放在我打算不修改的部分之后,即使指针是我希望保持不变的部分,我也保持一致性。

It works because both are same. 它的工作原因是两者都相同。 May be you confused in this, 可能你对此感到困惑,

const char*  // both are same
char const*

and

char* const  // unmutable pointer to "char"

and

const char* const  // unmutable pointer to "const char"

[To remember this, here is a simple rule, '*' affects its whole LHS first ] [要记住这一点,这是一个简单的规则, '*'首先影响其整个LHS ]

That is because the rule is: 那是因为规则是:

RULE: const binds left, unless there is nothing on the left, then it binds right :) 规则: const绑定左边,除非左边没有任何东西,然后它绑定正确:)

so, look at these as: 所以,看看这些:

(const --->> char)*
(char <<--- const)*

both same! 两者一样! oh, and --->> and <<--- are NOT operators, they just show what the const binds to. 哦,和--->><<---不是运算符,它们只显示const绑定的内容。

(from 2 simple variable initialization question ) (来自2个简单变量初始化问题

A really good rule of thumb regarding const : 关于const一个非常好的经验法则:

Read Declarations Right-to-Left. 从右到左阅读声明。

(see Vandevoorde/Josutiss "C++ Templates: The Complete Guide") (参见Vandevoorde / Josutiss“C ++模板:完整指南”)

Eg: 例如:

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.

Ever since I follow this rule-of-thumb, I never misinterpreted such declarations again. 自从我遵循这个经验法则之后,我再也没有误解过这样的声明。

(: sisab retcarahc-rep a no ton ,sisab nekot-rep a no tfel-ot-thgir naem I hguohT :tidE (:sisab retcarahc-rep a no ton,sisab nekot-rep a tfel-ot-thgir naem I hguohT:tidE

Here is how I always try to interpret: 以下是我总是试图解释的方式:

char *p

     |_____ start from the asterisk. The above declaration is read as: "content of `p` is a `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

           |_____ again start from the asterisk. "content of `p` is a constant `char`".

Hope it helps! 希望能帮助到你!

In both of your cases you're pointing to a constant char. 在两种情况下,您都指向一个常量字符。

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?

By default, const applies to what is inmediately at is left, but it could apply to what is inmediately at its right if there's nothing preceeding it, as in (1). 默认情况下, const适用于剩下的内容,但如果在它之前没有任何内容,它可以应用于其右边的内容,如(1)中所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM