关于const char*, char const* and char *const 等的区别

时间:2021-11-07 05:29:02

 1 关于const char*, char const* and char *const 等的区别
  2
  3 助记方法:
  4     把一个声明从右向左读
  5 比如:
  6 char * const cp;
  7 //cp is a const pointer to char
  8
  9 const char * cp;
 10 //cp is a pointer to const char;
 11 规则:
 12 //从右到左读:
 13 // * 读成 pointer to
 14 // ** 读成 (a) pointer to (a) pointer to
 15 // * const 读成 const pointer to
 16 再比如:
 17 char ** cpp; //cpp is a pointer to ( a pointer to char )
 18
 19 const char ** cpp; // cpp is a pointer to ( a pointer to const char )
 20
 21 char * const * cpp; //cpp is a pointer to const pointer to char
 22
 23 const char * const * cpp; //cpp is pointer to const pointer to const char
 24
 25 char ** const cpp; //cpp is const pointer to pointer to char
 26
 27 char * const * const cpp;//cpp is const pointer to const pointer to char
 28
 29 const char * const * const cpp; //cpp is const pointer to const pointer to c
onst char
 30
 31
 32 //×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
 33 //关键:
 34 //只要记住:
 35 //(1)把声明从右到左读
 36 //(2)* 读成 pointer to
 37 //×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××