課程
/后端開(kāi)發(fā)
/C++
/C++遠(yuǎn)征之離港篇
?const?int*是不可變地址的指針int?const*是指向不可變整數(shù)的指針
這是什么意思
2018-07-13
源自:C++遠(yuǎn)征之離港篇 3-4
正在回答
區(qū)別主要在于const位于*的前后
const位于*前,表示不能通過(guò)指針去改變?cè)撝羔標(biāo)缸兞康闹?但是可以用變量修改值,指針?biāo)傅淖兞恳彩强梢愿淖兊睦?
const int *p=&i;?
i=2;
這種寫(xiě)法是正確的;
*p=2;
這種寫(xiě)法是錯(cuò)誤的;
若修改指針?biāo)缸兞康闹?可以直接修改:
i=24;
const位于*后面,則指針一旦得到某個(gè)變量的地址,不能再指向其他的變量
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 提問(wèn)者
const int *p 與int const *p是一個(gè)意思啊,你要問(wèn)的是const int *p和 int * const p吧
舉報(bào)
C++掃清通往面向?qū)ο蟮淖詈笠坏勒系K,將所有知識(shí)點(diǎn)融會(huì)貫通
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2018-07-20
區(qū)別主要在于const位于*的前后
const位于*前,表示不能通過(guò)指針去改變?cè)撝羔標(biāo)缸兞康闹?但是可以用變量修改值,指針?biāo)傅淖兞恳彩强梢愿淖兊睦?
const int *p=&i;?
i=2;
這種寫(xiě)法是正確的;
*p=2;
這種寫(xiě)法是錯(cuò)誤的;
若修改指針?biāo)缸兞康闹?可以直接修改:
i=24;
const位于*后面,則指針一旦得到某個(gè)變量的地址,不能再指向其他的變量
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是一個(gè)意思啊,你要問(wèn)的是const int *p和 int * const p吧