有什么区别:

char * const 

and

const char *

当前回答

我猜你指的是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 *

// 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’
}

我想指出,使用int const *(或const int *)不是关于指向const int变量的指针,而是这个变量对于这个特定的指针是const的。

例如:

int var = 10;
int const * _p = &var;

上面的代码可以很好地编译。_p指向一个const变量,尽管var本身不是常量。

这里x基本上是一个字符指针,指向一个常数值 Char * const x是一个字符指针,它是一个常量,但是它指向的位置可以改变。 Const char* Const x是1和2的组合,意味着它是一个指向常量值的常量字符指针。 Const *char x将导致编译器错误。它不能被宣布。 Char const * x等于点1。

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

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

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

还有一个

const char * const

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

注意:

以下两种形式是等价的:

const char *

and

char const *

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

char const

over

const char

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