1. const指針總是指向相同的地址,該地址是不能被改變的.
1: int nValue = 5;
2: int *const pnPtr = &nValue;
*pnPtr = 6; 這樣的操作是可行的;而 int nValue2 = 2; pnPtr = &nValue2;這樣的操作是不可行的。
int *const pnPtr 可以這么理解,pnPtr當作地址,該指針有const地址,并且指向一個整型變量。
2. 指向const變量(雖然這個變量本身可以不是const的)的指針
1: int nValue = 5;
2: const int *pnPtr = &nValue;
const int *pnPtr可以這么理解,一個可以改變地址的指針指向的是一個不能改變大小的變量。
也就是說,*pnPtr = 6;這樣是不行的;int nValue2 = 6; pnPtr = &nValue2;這樣是可行的
3. 當然,還有一種就是兩者都不能改變了。
int nValue = 2;
const int *const pnPtr = &nValue;
不過此時可以通過nValue=6;來改變*pnPtr的值。把int nValue = 2;改成 const int nValue = 2;那么就什么都不能變咯。