有什么区别:

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

其他回答

Const char*是一个指向常量字符的指针 Char * const是一个指向字符的常量指针 Const char* Const是一个指向常量字符的常量指针

第一个是语法错误。也许你指的是两者的区别

const char * mychar

and

char * const mychar

在这种情况下,第一个指针是指向不能更改的数据的指针,第二个指针将始终指向相同的地址。

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

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

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


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总是修改在它之前的东西(在它的左边),除非它是类型声明中的第一个东西,在那里它修改在它之后的东西(在它的右边)。

所以这两个是一样的

int const *i1;
const int *i2;

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

这样的:

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

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