有什么区别:

char * const 

and

const char *

当前回答

// Some more complex constant variable/pointer declaration.
// Observing cases when we get error and warning would help
// understanding it better.

int main(void)
{
  char ca1[10]= "aaaa"; // char array 1
  char ca2[10]= "bbbb"; // char array 2

  char *pca1= ca1;
  char *pca2= ca2;

  char const *ccs= pca1;
  char * const csc= pca2;
  ccs[1]='m';  // Bad - error: assignment of read-only location ‘*(ccs + 1u)’
  ccs= csc;    // Good

  csc[1]='n';  // Good
  csc= ccs;    // Bad - error: assignment of read-only variable ‘csc’

  char const **ccss= &ccs;     // Good
  char const **ccss1= &csc;    // Bad - warning: initialization from incompatible pointer type

  char * const *cscs= &csc;    // Good
  char * const *cscs1= &ccs;   // Bad - warning: initialization from incompatible pointer type

  char ** const cssc=   &pca1; // Good
  char ** const cssc1=  &ccs;  // Bad - warning: initialization from incompatible pointer type
  char ** const cssc2=  &csc;  // Bad - warning: initialization discards ‘const’
                               //                qualifier from pointer target type

  *ccss[1]= 'x'; // Bad - error: assignment of read-only location ‘**(ccss + 8u)’
  *ccss= ccs;    // Good
  *ccss= csc;    // Good
  ccss= ccss1;   // Good
  ccss= cscs;    // Bad - warning: assignment from incompatible pointer type

  *cscs[1]= 'y'; // Good
  *cscs= ccs;    // Bad - error: assignment of read-only location ‘*cscs’
  *cscs= csc;    // Bad - error: assignment of read-only location ‘*cscs’
  cscs= cscs1;   // Good
  cscs= cssc;    // Good

  *cssc[1]= 'z'; // Good
  *cssc= ccs;    // Bad - warning: assignment discards ‘const’
                 //                qualifier from pointer target type
  *cssc= csc;    // Good
  *cssc= pca2;   // Good
  cssc= ccss;    // Bad - error: assignment of read-only variable ‘cssc’
  cssc= cscs;    // Bad - error: assignment of read-only variable ‘cssc’
  cssc= cssc1;   // Bad - error: assignment of read-only variable ‘cssc’
}

其他回答

我记得捷克的一本关于C的书:读声明,你从变量开始向左走。 因此,对于

char * const a;

你可以读成:“a是指向char的常量指针类型的变量”,

char const * a;

你可以读成:“a是一个指向char类型常量变量的指针。我希望这能有所帮助。

奖金:

const char * const a;

您将读取为指向char类型的常量变量的is常量指针。

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


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;是无效的。

区别在于const char *是指向const char类型的指针,而char * const是指向char类型的常量指针。

首先,被指向的值不能改变,但指针可以改变。第二,被指向的值可以改变,但指针不能改变(类似于引用)。

还有一个

const char * const

它是一个指向常量char的常量指针(因此它的任何内容都不能更改)。

注意:

以下两种形式是等价的:

const char *

and

char const *

具体的原因在c++标准中有描述,但重要的是要注意并避免混淆。我知道一些编码标准更喜欢:

char const

over

const char

(带或不带指针),这样const元素的位置与使用指针const时相同。

我猜你指的是const char *和char * const。

第一个const char *是一个指向常量字符的指针。指针本身是可变的。

第二个,char * const是一个指向字符的常量指针。指针不能改变,但它所指向的字符可以。

然后是const char * const,指针和字符不能改变。

const修饰符应用于紧挨着它左边的项。唯一的例外是,当它的左边没有任何东西时,它就适用于它右边的东西。

这些都是“指向常量char的常量指针”的等效方式:

Const char * Const Const char Const * Char const * const Char const const *