課程
/后端開發(fā)
/C++
/C++遠(yuǎn)征之離港篇
改成int const a=3;int const *p=&a;對(duì)否?如果不對(duì),為什么?
2015-07-15
源自:C++遠(yuǎn)征之離港篇 3-3
正在回答
const int const *p 這樣聲明是錯(cuò)的,這兩個(gè)const都是修飾int的,所以重復(fù)了。
應(yīng)該寫成 const int *p 或 int const *p,這兩種寫法是一樣的,都是將p聲明為指向const int的指針。
我猜你想比較的是 const int * const p。這樣是將p聲明為指向const int的const指針,就是說(shuō)p本身也是const的。
const?int?const?*p?=?&a; 這樣是可以編譯的
舉報(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)
2015-07-15
const int const *p 這樣聲明是錯(cuò)的,這兩個(gè)const都是修飾int的,所以重復(fù)了。
應(yīng)該寫成 const int *p 或 int const *p,這兩種寫法是一樣的,都是將p聲明為指向const int的指針。
我猜你想比較的是 const int * const p。這樣是將p聲明為指向const int的const指針,就是說(shuō)p本身也是const的。
2017-03-16