天天看點

【C++注意事項】4 指針 Pointers

和上一節的引用類似,指針(pointer)是“指向(point to)”另外一種類型的複合類型。與引用類似,指針也實作了對其他對象的間接通路。然後指針和引用相比有許多不同。其一,指針本身就是一個對象,允許對指針指派和拷貝,而且在指針的生命周期内它可以先後指向幾個不同的對象。其二,指針無須在定義時賦初值。和其他内置類型一樣,在塊作用域内定義的指針如果沒有被初始化,也将擁有一個不确定的值。

因為引用不是對象,沒有實際位址,是以不能定義指向引用的指針。

指針的值(即位址)應屬下列4種狀态之一:

1)指向一個對象

2)指向緊鄰對象所占空間的下一個位置

3)空指針,意味着指針沒有指定任何對象

4)無效指針,也就是上述情況之外的其他值

試圖拷貝或以其他方式通路無效指針的值都将引發錯誤。編譯器并不負責檢查此類錯誤,這一點和試圖使用未經初始化的變量是一樣的。通路無效指針的後果無法預計,是以程式員必須清楚任意給定的指針是否有效。是以解引用符也隻适用于那些确實指向了某個對象的有效指針。

所謂的解引用符就是使用操作符*來通路指針指向的對象。

取位址符(&)和解引用符(*)的多重含義:

空指針(null pointer)不指向任何對象,在試圖使用一個指針之前代碼可以檢查它是否為空。下面有3種生成空指針的方式:

過去的程式會用到一個名為null的預處理變量(preprocessor variable)來給指針指派,這個變量在頭檔案cstdlib中定義,它的值就是0。

直接将p2初始化為字面常量0

這是c++11新标準所引入的一種方法。

需要注意的是不能将int變量直接指派給指針,即便int變量的值恰好等于0也不行。

void*是一種特殊的指針類型,可用于存放任意對象的位址。

但是不能直接操作void*指針,因為我們并不知道這個對象到底是什麼類型,也就無法确定能在這個對象上做哪些操作。

english…

with two exceptions, which we will see at later, the types of the pointer and the object to which it points must match:

the types must match because the type of the pointer is used to infer the type of the object to which the pointer points. if a pointer addressed an object of another type, operations performed on the underlying object would fail.

as with reference, we can define pointers that point to either const or not const types. like a reference to const, a pointer to const may not be used to change the object to which the pointer points. we may store the address of a const object only in a pointer to const:

the first exception is that we can use a pointer to const to point to a nonconst object:

defining a pointer as a pointer to const affects only what we can do with the pointer. it is important to remenber that there is no guarantee that an object pointed to by a pointer to const won’t change.

it may be helpful to think of pointers and reference to const as pointers or references “that think they point to refer to const.”

const pointers

unlike references, pointers are objects. hence, as with any other object type, we can have a pointer that is itself const. like any other const object, a const pointer must be initialized, and once initialized, its value(i.e., the address that it holds) may not be changed. we indicate that the pointer is const by putting the const after the *. this placement indicates that it is the pointer, not the pointed-to type, that is const:

the symbol closest to curerr is const, which means that curerr itself will be a const object. the type of that object is formed from the rest of the declarator. the next symbol in the declarator is *, which means that curerr is a const pointer. finally, the base type of the declaration completes type of curerr, which is a const pointer to an object of type int. similarly, pip is a const pointer to an object of type const double.

the fact that a pointer is itself const says nothing about whether we can use the pointer to change the underlying object. whether we can change that object depends entirely on the type to which the pointer points. for example, pip is a const pointer to const. neither the value of the object addressed by pip nor the address stored in pip can be changed. on the other hand, curerr addresses a plain, nonconst int. we can use curerr to change the value of errnumb:

note: “c++ notes” series of blog mainly reference books of c++ prime is the fifth edition of the chinese version and english version. my english is very poor, and i don’t want there are some syntax error in blog. so the blogs have lots of words and sentences from the book. i wrote this series in order to consolidate the knowledge of c++, but also in order to improve my poor english. i also hope to help readers. thank you.、

感謝您的通路,希望對您有所幫助。 歡迎大家關注、收藏以及評論。

我的更多部落格文章:nomasp部落格導讀

為使本文得到斧正和提問,轉載請注明出處:

http://blog.csdn.net/nomasp

繼續閱讀