wor*_*ad3 339

不同的是,const char *是一个指针指向一个const char,而char * const是一个常数指针char.

第一个,指向的值不能改变,但指针可以.第二个,指向的值可以改变,但指针不能(类似于引用).

还有一个

const char * const
Run Code Online (Sandbox Code Playgroud)

这是一个指向常量char的常量指针(因此无法更改它).

注意:

以下两种形式是等效的:

const char *
Run Code Online (Sandbox Code Playgroud)

char const *
Run Code Online (Sandbox Code Playgroud)

C++标准中描述了其确切原因,但重要的是要注意并避免混淆.我知道几种编码标准更喜欢:

char const
Run Code Online (Sandbox Code Playgroud)

过度

const char
Run Code Online (Sandbox Code Playgroud)

(带或不带指针),以便const元素的位置与指针相同const.

  • 是否值得注意如果在同一声明中指定了多个变量会发生什么?我相信`const int*foo,*bar;`会声明`foo`和`bar`都是`int const*`,但是`int const*foo,*bar`会声明`foo`是`int const*`和`bar`是`int*`.我认为`typedef int*intptr; const intptr foo,bar;`将两个变量声明为`int*const`; 我不知道如何使用组合声明在没有typedef的情况下创建该类型的两个变量. (6认同)
  • @supercat(哦,C-only,对不起C++代码链接,我从C++问题到这里)这是关于_C声明语法_,带有("纯")_type_部分后跟_declarator_.在"`int const*foo,*volatile bar`"中,类型部分是`int const`(在`*`之前停止),声明符是`*foo`(表达式`*foo`将表示`int const `)和`*volatile bar`; 读_right-to-left_(_cv-qualifiers_的好规则),`foo`是指向_const_ int的指针,`bar`是指向_const_ int的_volatile_指针(指针本身是易失性的,指向的int是[访问为] const). (3认同)
  • @supercat `我相信 const int *foo,*bar; 将声明 foo 和 bar 为 int const *`:是的。`但是 int const *foo, *bar 会将 foo 声明为 int const * 并将 bar 声明为 int *`:_不!_ 这将与前面的情况完全相同。(请参阅 http://ideone.com/RsaB7n,其中 foo 和 bar 都会出现相同的错误)。`我认为 typedef int * intptr; const intptr foo,bar; 会将两个变量声明为 int * const`:是的。`我不知道有什么方法可以使用组合声明在没有 typedef 的情况下创建该类型的两个变量`:嗯,`int *const foo, *const bar;`。C 声明符语法... (2认同)

dia*_*pir 96

为避免混淆,请始终附加 const限定符.

int       *      mutable_pointer_to_mutable_int;
int const *      mutable_pointer_to_constant_int;
int       *const constant_pointer_to_mutable_int;
int const *const constant_pointer_to_constant_int;
Run Code Online (Sandbox Code Playgroud)

  • @Andrew:我暗示了一致性和可读性.编写所有类型限定符,以便修改左边的内容_always_,就是我使用的内容. (12认同)
  • 为什么?"避免混淆"并不能解释我的困惑. (9认同)
  • 作为代码标准,我很少遇到这种风格,因此不太可能采用它.然而,作为一种学习工具,这个答案非常有用!(所以我觉得太糟糕了,这不是更常见的风格.) (8认同)
  • @Alla:`p`与类型无关:`(const int*const)`.无论好坏,(更糟糕的是,如果你问我)const限定符,无论是在C和C++中,都是postfix:cf const member function`void foo(int a)const;`.声明`const int`的可能性是例外而不是规则. (7认同)
  • 实际上,这是我在 SO 中找到的关于该主题的最佳答案 (2认同)

Don*_*hey 41

const 总是修改它之前的东西(在它的左边),除了它是类型声明中的第一件事,它修改了它之后的东西(在它的右边).

所以这两个是一样的:

int const *i1;
const int *i2;
Run Code Online (Sandbox Code Playgroud)

他们定义指向a的指针const int.您可以更改位置i1i2点,但不能更改它们指向的值.

这个:

int *const i3 = (int*) 0x12345678;
Run Code Online (Sandbox Code Playgroud)

定义const指向整数的指针并将其初始化为指向内存位置12345678.您可以更改int地址12345678处的值,但不能更改i3指向的地址.


Ada*_*eld 22

const * char是无效的C代码,没有意义.也许你的意思是要问a const char *和a 之间的区别char const *,或者可能是a const char *和a 之间的区别char * const

也可以看看:


And*_*son 14

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


Mr.*_*ama 9

经验法则:从右到左阅读定义!


const int *foo;

意味着" foo点(*)到一个int不能改变(const)".
为了程序员,这意味着"我不会改变的价值是什么foo指向".

  • *foo = 123;或者foo[0] = 123;无效.
  • foo = &bar; 被允许.

int *const foo;

意思是" foo不能改变(const)和点(*)到int".
为了程序员,这意味着"我不会改变的内存地址foo指".

  • *foo = 123;或被foo[0] = 123;允许.
  • foo = &bar; 会无效的.

const int *const foo;

意味着" foo不能改变(const)和点(*)到int不能改变(const)".
为了程序员,这意味着"我不会改变价值的东西foo指向,我也不会改变地址foo指".

  • *foo = 123;或者foo[0] = 123;无效.
  • foo = &bar; 会无效的.


AAn*_*kit 8

1)const char*x这里X基本上是一个指向常量值的字符指针

2)char*const x指的是常量的字符指针,但它指向的位置可以改变.

3)const char*const x是1和2的组合,意味着它是一个指向常量值的常量字符指针.

4)const*char x将导致编译器错误.它不能被宣布.

5) char const*x等于1.

经验法则是如果const是带有var名称,那么指针将是常量但指向位置可以改变,否则指针将指向一个恒定位置,指针可以指向另一个位置,但指向位置内容不能改变.

  • “char* const x 是指字符指针,它是常量,但它指向的位置可以改变。” 错误的。可以更改该位置的值,但不能更改位置本身。 (2认同)

Aad*_*hri 7

另一个经验法则是检查const 的位置:

  1. before * =>存储的常量
  2. after * =>指针本身是常量