char * const和const char *有什么区别?
有什么区别:
char * const 和
 const char * 
	
 不同的是, const char *是一个指向const char *的指针,而char * const是一个指向char的常量指针。 
第一,指向的值不能改变,但指针可以。 第二,指向的值可以改变,但指针不能(类似于引用)。
还有一个
 const char * const 
这是一个常量字符的常量指针(所以没有关于它可以改变)。
注意:
以下两种形式是等价的:
 const char * 
和
 char const * 
确切的原因在C ++标准中有描述,但要注意并避免混淆。 我知道几个更喜欢的编码标准:
 char const 
过度
 const char 
  (带或不带指针),以便const元素的位置与指针const相同。 
为了避免混淆,总是附加 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总是修改前面的东西(在它的左边),除非它是类型声明中的第一个东西,它修改后面的东西(在它的右边)。 
所以这两个是一样的:
 int const *i1; const int *i2; 
 他们定义指向一个const int指针。 你可以改变i1和i2位置,但是你不能改变他们指向的值。 
这个:
 int *const i3 = (int*) 0x12345678; 
 定义了一个指向一个整数的const指针,并将其初始化为指向内存位置12345678.您可以更改地址12345678处的int值,但不能更改i3指向的地址。 
  const * char是无效的C代码,是毫无意义的。 也许你打算问一下const char *和char const *之间的区别,或者可能是const char *和char * const之间的区别? 
也可以看看:
- 什么是常量指针(而不是指向常量对象的指针)?
- C中的Const
- C ++中的常量声明之间的区别
- C ++ const问题
- 为什么我可以改变一个const char *变量的值?
  const char*是一个指向常量字符的指针 
  char* const是一个char* const的常量指针 
  const char* const是一个指向常量字符的常量指针 
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名称,那么指针将是常量,但指针位置可以改变 ,否则指针将指向一个常量位置,指针可以指向另一个位置,但指向位置的内容不能改变 。
第一个是语法错误。 也许你的意思是区别
 const char * mychar 
和
 char * const mychar 
在这种情况下,第一个是指向不能改变的数据的指针,第二个指针总是指向相同的地址。
另一个拇指规则是检查const在哪里:
- 之前* =>存储的值是不变的
- * => 指针本身是不变的
经验法则:从右向左阅读定义!
 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 *和char * const。
第一个const char *是一个指向常量字符的指针。 指针本身是可变的。
第二个,char * const是一个字符的常量指针。 指针不能改变,它指向的字符可以。
然后const char * const指针和字符不能改变。
这里是代码的详细解释
 /*const char * p; char * const p; const char * const p;*/ // these are the three conditions, // const char *p;const char * const p; pointer value cannot be changed // char * const p; pointer address cannot be changed // const char * const p; both cannot be changed. #include<stdio.h> /*int main() { const char * p; // value cannot be changed char z; //*p = 'c'; // this will not work p = &z; printf(" %c\n",*p); return 0; }*/ /*int main() { char * const p; // address cannot be changed char z; *p = 'c'; //p = &z; // this will not work printf(" %c\n",*p); return 0; }*/ /*int main() { const char * const p; // both address and value cannot be changed char z; *p = 'c'; // this will not work p = &z; // this will not work printf(" %c\n",*p); return 0; }*/ 
- 常量指针 :在整个程序中,常量指针只能指向相应数据类型的单个变量。我们可以改变指针所指向的变量的值。 初始化应该在声明本身的时候完成。
句法:
 datatype *const var; 
  char *const在这种情况下。 
 /*program to illustrate the behaviour of constant pointer */ #include<stdio.h> int main(){ int a=10; int *const ptr=&a; *ptr=100;/* we can change the value of object but we cannot point it to another variable.suppose another variable int b=20; and ptr=&b; gives you error*/ printf("%d",*ptr); return 0; } 
- 指向const值的指针:在这个指针可以指向任何数量的相应类型的变量,但我们不能改变在指定的时间指针指向的对象的值。
句法:
  const datatype *var或datatype const *var 
  const char*在这种情况下。 
 /* program to illustrate the behavior of pointer to a constant*/ #include<stdio.h> int main(){ int a=10,b=20; int const *ptr=&a; printf("%d\n",*ptr); /* *ptr=100 is not possible ie we cannot change the value of the object pointed by the pointer*/ ptr=&b; printf("%d",*ptr); /*we can point it to another object*/ return 0; } 
char * const和const char *?
- 指向一个恒定的价值
 const char * p;  //值不能改变 
- 常量指向一个值
 char * const p;  //地址不能改变 
- 常量指针指向一个常量值
 const char * const p;  //两者都不能改变。 
  const修饰符被应用到它的左边。 唯一的例外是当它没有任何东西在左边时,那么它适用于右边的东西。 
 这些都是“常量char常量指针”的等价方法: 
-  const char * const
-  const char const *
-  char const * const
-  char const const *
两条规则
-  If const is between char and *, it will affect the left one.
-  If const is not between char and *, it will affect the nearest one.
例如
-  char const *. This is a pointer points to a constant char.
-  char * const. This is a constant pointer points to a 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 *a; 
 根据顺时针/螺旋规则, a是指向常量的字符的指针。 这意味着字符是恒定的,但指针可以改变。 即a = "other string"; 很好,但是a[2] = 'c'; 将无法编译 
B)
 char * const a; 
 按照规则, a是一个字符的const指针。 即你可以做a[2] = 'c'; 但你不能做a = "other string";