const指针速记

const 出现在 * 的左边,表示被指物是常量。

例如:const int * a; int const * a; 两者表达同样的意思:指针a所指向的值不可被修改,但a可以指向其他地址。

const 出现在 * 的右边,表示指针是常量。

例如:int * const a; 指针a不能指向其他地址,但是可以修改a所指向的地址里面的值。

const 出现在 * 的两边,表示指针和被指物都是常量。

例如:const int * const a; 地址和值都不能被改变。

 

Read it backwards (as driven by Clockwise/Spiral Rule):

  • int* - pointer to int
  • int const * - pointer to const int
  • int * const - const pointer to int
  • int const * const - const pointer to const int

Now the first const can be on either side of the type so:

  • const int * == int const *
  • const int * const == int const * const

If you want to go really crazy you can do things like this:

  • int ** - pointer to pointer to int
  • int ** const - a const pointer to a pointer to an int
  • int * const * - a pointer to a const pointer to an int
  • int const ** - a pointer to a pointer to a const int
  • int * const * const - a const pointer to a const pointer to an int
posted @ 2020-09-02 14:47  今天星期五了  阅读(127)  评论(0)    收藏  举报