本文鏈接:http://blog.csdn.net/ncepuwanghui/article/details/23712735
#include <iostream>
using namespace std;
void func(char* p)
{
p = new char[128];
//*p = "Hello World";//錯誤寫法:invalid conversion from ‘const char*’ to ‘char’
p = "Hello World";//有警告:將一個字符串常量轉換爲char*時存在風險,應使用常量指針
cout << "&p = " << &p << endl;
cout << "p = " << (void*)p << endl;
cout << "*p = " << *p << endl;
cout << "p = " << p << endl;
}
int main(int argc, char const *argv[])
{
char *ptr = NULL;
//char *ptr = 0;
cout << "-------------------------------------------------" << endl;
cout << "&ptr = " << &ptr << endl;//存放指針ptr的單元地址
cout << "ptr = " << (void*)ptr << endl;//指針ptr指向的單元地址
//cout << "*ptr = " << *ptr << endl;//段錯誤,無法訪問0x0地址單元
cout << "ptr = " << ptr << endl;//指針ptr所指單元內容,而不是指針ptr指向的單元地址
//此處由於ptr是空指針,ptr所指單元內容爲0,與字符串結束符'\0'值相同
//因此當輸出ptr內容後輸出終止,不會輸出後面的“<<endl”
//cout在輸出前要調用strlen來取得字符串的長度,不可避免得要有讀字符串的操作
//也就是讀了內存地址爲0,導致輸出流異常,可用cout.clear();重置輸出流
//if (!cout)
//if (cout.bad())
if (cout.fail())
{
cout.clear();//設置內部錯誤狀態,如果用缺省參量調用,則清除所有錯誤位
cout << "\nOutput stream aready normal" << endl;
}
//執行結果如下:
/*
* [wanghui@wanghui 0409]$ g++ -o exe func.cpp && ./exe
* &ptr = 0xbfd78f6c
* ptr = 0
* ptr = [wanghui@wanghui 0409]$
*/
/*
*C++的IO流是通過操作符"重載"來實現的.
* OS& operator <<(OS&, char*) // 輸出字符串的函數
* OS& operator <<(OS&, void*) // 輸出指針地址的函數
* 當cout << p時; 如果p是個char*, 那麼就會調用前一個函數, 用字符串方式輸出.
* 如果p是其他的指針, 包括void*,int*等等; 都會匹配第二個函數, 用指針方式輸出.
* cout爲流對象,自動識別輸出數據的類型.
* 對於字符地址,即指針類型,就是輸出其所在地址區間的所有字符,直到字符串結束符'\0'爲止.
*/
cout << "-------------------------------------------------" << endl;
func(ptr);
cout << "-------------------------------------------------" << endl;
cout << "&ptr = " << &ptr << endl;//存放指針ptr的單元地址
cout << "ptr = " << (void*)ptr << endl;//指針ptr指向的單元地址
//cout << "*ptr = " << *ptr << endl;//段錯誤,無法訪問0x0地址單元
cout << "ptr = " << ptr << endl;//指針ptr所指單元內容,而不是指針ptr指向的單元地址
//if (!cout)
//if (cout.bad())
if (cout.fail())
{
cout.clear();//設置內部錯誤狀態,如果用缺省參量調用,則清除所有錯誤位
cout << "\nOutput stream aready normal" << endl;
}
//delete ptr;
delete[] ptr;
//測試其他數據類型空指針
cout << "-------------------------------------------------" << endl;
int *p = NULL;
cout << "&p = " << &p << endl;//存放指針p的單元地址
//cout << "p = " << (void*)p << endl;
cout << "p = " << p << endl;//指針p指向的單元地址
//cout << "*p = " << *p << endl;//段錯誤,無法訪問0x0地址單元
return 0;
}
運行結果如下:
[wanghui@wanghui 0409]$ g++ -o exe func.cpp
func.cpp: In function ‘void func(char*)’:
func.cpp:19: warning: deprecated conversion from string constant to ‘char*’
[wanghui@wanghui 0409]$ ./exe
-------------------------------------------------
&ptr = 0xbfb2da2c
ptr = 0
ptr =
Output stream aready normal
-------------------------------------------------
&p = 0xbfb2da10
p = 0x8048d44
*p = H
p = Hello World
-------------------------------------------------
&ptr = 0xbfb2da2c
ptr = 0
ptr =
Output stream aready normal
-------------------------------------------------
&p = 0xbfb2da28
p = 0
參考文章:
http://www.cplusplus.com/forum/beginner/74372/
http://bytes.com/topic/c/answers/494457-null-pointer-cout
http://blog.sina.com.cn/s/blog_70a5a49d01014qmr.html