您的位置:首页 > 编程语言 > C语言/C++

C++中cout/cin对于空指针、字符指针的处理,及输入>>、 输出<<操作符的重载

2014-04-14 23:05 489 查看
本文链接: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux c++ 指针 io流
相关文章推荐