2016-10-03 122 views
0

我對C相當陌生。有人可以向我解釋下面的區別嗎?就用法和概念而言。恆定指針與指向常量的指針之間的區別C

int *const p= &x; 

int const *p= &x; 

const int *const p= &x; 

或指針使用的任何其他變化,這將有助於我完全理解概念。

回答

1

該常量適用於1)指針本身 - 它是否可以在初始化後更改爲指向其他內容,以及2)指針指向的數據 - 數據是否可以通過指針更改。

int *const p= &x; // p is const pointer to non-const data - p cannot change to point to something else, but you can change what it points to 

int const *p= &x; // p is non-const pointer to const data - p can change to point to something else, but what it points to cannot be changed 

const int *const p= &x; // p is const pointer to const data - p cannot change to point to something else, and what it points to cannot be changed