有什么区别:

char * const 

and

const char *

当前回答

const总是修改在它之前的东西(在它的左边),除非它是类型声明中的第一个东西,在那里它修改在它之后的东西(在它的右边)。

所以这两个是一样的

int const *i1;
const int *i2;

它们定义指向const int类型的指针。你可以改变i1和i2的点,但你不能改变它们的值。

这样的:

int *const i3 = (int*) 0x12345678;

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

其他回答

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


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和*之间,它将影响最近的一个。

e.g.

Char const *。这是一个指向常量char的指针。 Char * const。这是一个指向char类型的常量指针。

为了避免混淆,总是附加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;

Const char*是一个指向常量字符的指针 Char * const是一个指向字符的常量指针 Const char* 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’
}