2012-02-28 141 views
2

可能重複:
what is the difference between const int*, const int * const, int const *這種類型究竟是什麼?

有人能解釋這種類型的給我?

char const * const blah; 

我本來期待它寫成類似於:const char *。 const發佈的類型是什麼意思?例如int const foo

然後,第一條語句是什麼意思?

+0

分號在哪裏?變量或函數或...名稱在哪裏? – 2012-02-28 17:19:06

+0

對不起,修正。主要問題是我的大腦完全被炸。 – SirYakalot 2012-02-28 17:20:52

+0

沒有任何賦值,這個類型*無用*。聲明後不能更改常量'blah'。 – 2012-02-28 17:22:32

回答

4

常量字符的常量指針。你不能改變它指向的地址,你不能改變它最後的字符。

+0

我很困惑,爲什麼它不是const int * const – SirYakalot 2012-02-28 17:22:06

+0

@SirYakalot:'const int foo'與'int const foo'完全相同。通過類似的邏輯,'const int * const foo'與'int const * const foo'完全相同。 – 2012-02-28 17:25:33

+0

我從來沒有新的,你可以把常量之前或之後。呵呵。那麼const int const *怎麼樣? – SirYakalot 2012-02-28 17:29:25

2
char const * const ptr = "Hello"; 

ptr是一個常量指針到一個恆定字符(多個)。這意味着ptr以及它所指向的數據都不能被修改。由於指針自身是常量類型,因此它需要初始化,因爲它以後不能被重新分配。

char const * const laterPtr; // Wrong.