課程
/后端開發(fā)
/C++
/C++遠征之離港篇
改成int const a=3;int const *p=&a;對否?如果不對,為什么?
2015-07-15
源自:C++遠征之離港篇 3-3
正在回答
const int const *p 這樣聲明是錯的,這兩個const都是修飾int的,所以重復(fù)了。
應(yīng)該寫成 const int *p 或 int const *p,這兩種寫法是一樣的,都是將p聲明為指向const int的指針。
我猜你想比較的是 const int * const p。這樣是將p聲明為指向const int的const指針,就是說p本身也是const的。
const?int?const?*p?=?&a; 這樣是可以編譯的
舉報
C++掃清通往面向?qū)ο蟮淖詈笠坏勒系K,將所有知識點融會貫通
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2015-07-15
const int const *p 這樣聲明是錯的,這兩個const都是修飾int的,所以重復(fù)了。
應(yīng)該寫成 const int *p 或 int const *p,這兩種寫法是一樣的,都是將p聲明為指向const int的指針。
我猜你想比較的是 const int * const p。這樣是將p聲明為指向const int的const指針,就是說p本身也是const的。
2017-03-16