2021年11月8日 星期一

[C] const

const是修飾右邊的內容

const int a;
a = 0xAA;
[Error] assignment of read-only variable 'a'


const int * a;	
a = 0xAA; //ok
*a = 0xAA; //error
[Error] assignment of read-only location '*a'


修飾指標變數a
int const * a;	
a = 0xAA; //ok
*a = 0xAA; //error
[Error] assignment of read-only location '*a'


修飾變數a
int * const a;	
a = 0xAA; //error
*a = 0xAA; //ok
[Error] assignment of read-only variable 'a'


修飾*和變數a
int const * const a;	
a = 0xAA;
*a = 0xAA;
[Error] assignment of read-only variable 'a'
[Error] assignment of read-only variable '*a'



#C語言#const範例#const教學

沒有留言:

張貼留言