課程
/后端開發
/C++
/C++遠征之離港篇
?const?int*是不可變地址的指針int?const*是指向不可變整數的指針
這是什么意思
2018-07-13
源自:C++遠征之離港篇 3-4
正在回答
區別主要在于const位于*的前后
const位于*前,表示不能通過指針去改變該指針所指變量的值,但是可以用變量修改值,指針所指的變量也是可以改變的例如:
const int *p=&i;?
i=2;
這種寫法是正確的;
*p=2;
這種寫法是錯誤的;
若修改指針所指變量的值,可以直接修改:
i=24;
const位于*后面,則指針一旦得到某個變量的地址,不能再指向其他的變量
const int *p =&x;<==> int const *p=&x;
*p=4; (any value) --> this is wrong!!!
p=&y (any address) -->this is correct!!!
So, const is just for "*p", not "p" in this example.
慕的地9298213 提問者
const int *p 與int const *p是一個意思啊,你要問的是const int *p和 int * const p吧
舉報
C++掃清通往面向對象的最后一道障礙,將所有知識點融會貫通
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2018-07-20
區別主要在于const位于*的前后
const位于*前,表示不能通過指針去改變該指針所指變量的值,但是可以用變量修改值,指針所指的變量也是可以改變的例如:
const int *p=&i;?
i=2;
這種寫法是正確的;
*p=2;
這種寫法是錯誤的;
若修改指針所指變量的值,可以直接修改:
i=24;
const位于*后面,則指針一旦得到某個變量的地址,不能再指向其他的變量
2018-07-19
const int *p =&x;<==> int const *p=&x;
*p=4; (any value) --> this is wrong!!!
p=&y (any address) -->this is correct!!!
So, const is just for "*p", not "p" in this example.
2018-07-13
const int *p 與int const *p是一個意思啊,你要問的是const int *p和 int * const p吧