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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了the difference between const int *, int * const, int const *相关的知识,希望对你有一定的参考价值。

  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 *的主要内容,如果未能解决你的问题,请参考以下文章

The difference between variance and bias

What are the differences between Perl, Python, AWK and sed

What are the differences between Perl, Python, AWK and sed

The difference between UDS on IP and UDS on CAN

What is the difference between btree and rtree indexing?

[Immutable.js] Differences between the Immutable.js Map() and List()