0:空指針
在C和C++中,有關指針的上下文中出現的0代表空指針:
int i =0; //i now contains theinteger zero
char*s=0; //s now contains the nullpointer
(注3: 不要認爲0是一個地址,儘管空指針實現時常被作爲地址0,但是實際上不必這樣。)
這裏0是用作整數的,它代表空指針。
爲了使意圖更爲明瞭,當要表示空指針時,許多程序員都使用空指針宏NULL:
/*Ccode*/
#defineNULL (void *)0 /*typical definition ofnull-pointer macro*/
Foo* x =NULL; /*works fine in c*/
NULL宏包括void*的類型轉換,以便解釋這個宏時,一直把它當做指針而不是整數,尤其是在函數調用的時候。但如前所述,在C++中如果沒有類型轉換,void* 是不能賦給任意的指針的。如果你想在C++中使用NULL宏,而且不想每次都進行類型轉換,那麼NULL必須被定義爲0:
//C++code
#define NULL 0 //most reasonable definitionof null-pointer //macro in C++
Foo *x = NULL; //works fine in C++
在C++中,使用0而不必進行到void*的類型轉換這一機制運
行的很好, 因爲無論你認爲它是指針還是整數,通常都會
在上下文中澄清。實際上,許多C++程序員都拋棄了宏而
在代碼中使用0作爲空指針。
int* p = 0; //many C++ programmersavoid NULL
也有一些程序員不願意拋棄自己所喜愛的宏。在本書中我們
使用0,因爲他已經成爲C++的標準了。你可能會在其他程
序員的代碼中見到NULL或0。
要慎用NULL的一個很好的原因是,當一個重載函數的參數
即可以是整數也可以是指針時,NULL就會引起麻煩。編譯器
會認爲0與整數更匹配,而不是指針:
#define NULL 0
void snafu (int i); //first snafu
void snafu (char* c); //second snafu
main () {
snafu(0); //danger: calls first snafu
//thisis not what you meant if you
//wereusing 0 as the null pointer
snafu(NULL); //error: calls first snafu
//this is almost certainly not //what youmeant
}
對函數snafu()的兩次調用使用的都是snafu(int)。最好不要對參數爲指針和整數的函數進行重載,這樣會引起混亂,除非你在調用函數時從不使用空指針。如果重載無法避免,那麼可以使用類型轉換:
voidsnafu (int i); //firstsnafu
voidsnafu (char* c) //second snafu
main () {
snafu((int) 0); //calls first snafu, the cast is unnecessary
//butmakes your intention clear
snafu((char *) NULL); //calls second snafu,the cast is //essential
}
我們在這裏加入了類型轉換以後告訴編譯器應該調用那
個一函數。