`

const char* 与char*const的区别

 
阅读更多

 C++标准规定,const关键字放在类型或变量名之前等价的。
const int n=5//same as below
int const m=10

const   char   *   pstr; //   pstr   是字符指针,它指向的量是   const的,
例如:   char   *m= "hello   world 1";
          
char   *n= "Hello   world 2";
          
const   char   *   pstr=m;
          pstr[
1]= '';   //   it 's   wrong
          pstr=n;             //   it 's   right,初始化以后可以更改

char*   const   pstr;//  pstr   是字符指针,这个指针的值必须初始化,初始化以后就不能改变了.
例如:   char   *m= "hello   world 3";
          
char   *n= "Hello   world 4";
          
char*   const   pstr=m;
          pstr
=n;   //   it 's   wrong
          pstr[1]= '';   //   it 's   right

至于   
const   char*   const   pstr   自然是两者的结合了.

 

转自:http://www.cppblog.com/aaxron/archive/2010/12/23/137298.html

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics