ALEXKK2011

The Technical Side of alexKK2011
  博客园  :: 新随笔  :: 订阅 订阅  :: 管理

pointer & NULL & constant variable

Posted on 2011-02-22 23:03  alexkk2011  阅读(157)  评论(0编辑  收藏  举报
#include <iostream>
#include <string>

using namespace std;

void main()
{
	//exp.4
	char  *charPtr=NULL;
	cout << "*charPtr = NULL" << endl;
	//cout << "*charPtr is : " << *charPtr << endl; //运行时错误:sizeof.exe 中的0x0041153b 处未处理的异常: 0xC0000005: 读取位置0x00000000 时发生访问冲突
	//cout << "charPtr is : " << charPtr << endl; //运行时错误:sizeof.exe 中的0x0041153b 处未处理的异常: 0xC0000005: 读取位置0x00000000 时发生访问冲突
	cout << "&charPtr is : " << &charPtr << endl;
	cout << "sizeof(charPtr) is : " << sizeof(charPtr) << endl;
	cout << "sizeof(*charPtr) is : " << sizeof(*charPtr) << endl << endl;
	//*charPtr = ""; //编译错误无法从“const char [1]”转换为“char”
	
	charPtr = ""; 
	cout << "charPtr = \"\"" << endl;
	cout << "*charPtr is : " << *charPtr << endl;
	cout << "charPtr is : " << charPtr << endl;
	cout << "&charPtr is : " << &charPtr << endl;
	cout << "sizeof(charPtr) is : " << sizeof(charPtr) << endl;
	cout << "sizeof(*charPtr) is : " << sizeof(*charPtr) << endl << endl; 

	charPtr = "a"; 
	cout << "charPtr = \"a\"" << endl;
	cout << "*charPtr is : " << *charPtr << endl;
	cout << "charPtr is : " << charPtr << endl;
	cout << "&charPtr is : " << &charPtr << endl;
	cout << "sizeof(charPtr) is : " << sizeof(charPtr) << endl;
	cout << "sizeof(*charPtr) is : " << sizeof(*charPtr) << endl << endl;

	/*
	*charPtr = NULL
	&charPtr is : 0012FF60
	sizeof(charPtr) is : 4
	sizeof(*charPtr) is : 1

	charPtr = ""
	*charPtr is : 
	charPtr is :
	&charPtr is : 0012FF60
	sizeof(charPtr) is : 4
	sizeof(*charPtr) is : 1

	charPtr = "a"
	*charPtr is : a
	charPtr is : a
	&charPtr is : 0012FF60
	sizeof(charPtr) is : 4
	sizeof(*charPtr) is : 1

	请按任意键继续. . .
	*/
}