說一個比較好記的方法來區(qū)分 int const *p與 int* const p,把*讀作pointer to然后從后往前讀.
第一個int const *p就可以讀作 p is a pointer to const int,p是指向常量的指針
第二個int* const p就可以讀作 p is a const pointer to int,p是指向int型的常指針
第一個int const *p就可以讀作 p is a pointer to const int,p是指向常量的指針
第二個int* const p就可以讀作 p is a const pointer to int,p是指向int型的常指針
2015-08-03
可變的指針去指向不可變的變量是錯的
const int *p 常指針
int *const p 指向常量的指針
const int *const p 指向常量的常指針
const int *p 常指針
int *const p 指向常量的指針
const int *const p 指向常量的常指針
2015-08-03
可變的指針去指向不可變的變量是錯的
const int x=3;
int *y=&x;//錯誤
int x=3;
const int *y=&x;//正確
const int x=3;
int *y=&x;//錯誤
int x=3;
const int *y=&x;//正確
2015-08-02
int x=3;
const int*y=&x;
*y=5;
//單從語法上就是錯誤的
int x=3,z=6;
const int *y=&x;
y=&z//正確
const int*y=&x;
*y=5;
//單從語法上就是錯誤的
int x=3,z=6;
const int *y=&x;
y=&z//正確
2015-08-02