码迷,mamicode.com
首页 > 其他好文 > 详细

the difference between const int *, int * const, int const *

时间:2016-12-05 16:58:08      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:win   poi   position   conf   follow   ons   stp   statement   text   

  Some people may be confused about the sequence of const and * on declaration in C++/C, me too.

  Now I think we can distinguish them by this way:

  1.only noticing the position of const to *, and we can find that the following statements are same:    

const int * foo_int;
int const * foo_int_;  //same.

  2.regarding const as a postpositive attributes,and you will know the content which is const.

  

int foo = 6;
int foo_ = 7;

//the foo_int is const, but the pointer to foo_int can be chaged.
int const * foo_int = &foo;

//illegal, the value cannot be chaged
//*foo_int = 7

//okay!
foo_int = &foo_;

//the pointer to int is const, but foo_int_ can be chaged.
int * const foo_int_ = &foo_int;

//illegal, the pointer cannot be changed.
//foo_int = &foo_int_;
//even if the new pointer is same, it‘s illegal
//foo_int = &foo_int;

//okay
*foo_int = 8;

 

the difference between const int *, int * const, int const *

标签:win   poi   position   conf   follow   ons   stp   statement   text   

原文地址:http://www.cnblogs.com/maikaze/p/6134361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!