区别C中的指针常量和常量指针
C 语言中常常可以看到诸如 const int * ,const int * const,int const * 这样的写法。这些语法外表相似,实际却有很大不同。为了分辨它们,可以用从右向左阅读的技巧。
int *- pointer to int,指针指向整数int const *- pointer to const int,指针指向常量整数int * const- const pointer to int,常量指针指向整数int const * const- const pointer to const int,常量指针指向常量整数
第一个 const 和类型可以互换:
const int *==int const *const int * const==int const * const
如此一来,就算忘记了常量指针和指针常量谁是谁也没有关系,通过字面上的顺序就能分辨指针的不同。确实是一个很不错的方法!
参考资料
区别C中的指针常量和常量指针
https://tomzhu.site/2020/09/21/区别C中的指针常量和常量指针/