C++Primer 习题2.18-2.26

C++Primer 习题2.18-2.26

引用、指针、const

//2.18
//2.18
#include<iostream>
using namespace std;
int main()
{
	int i = 2, j = 3;
	int *p = &i;
	cout << p << "," << *p << endl;  //结果:地址1,2
	p = &j;		//更改所指对象
	cout << p << "," << *p << endl;	//结果:地址2(改变),3(改变)
	*p = 5;		//更改指针的值
	cout << p << "," << *p << endl;	//结果:地址2(未变),5
	j = 10;		//更改j的值
	cout << p << "," << *p << endl;	//结果:地址2(未变),10
	return 0;
}

//2.19
/*
引用与指针的区别:
相同点:	都是间接访问对象,为复合类型。
不同点:	1.指针更加灵活可以改变指向访问的对象,而引用不行“即绑定”到一个指定的对象上
				2.指针不必须进行初始化,而引用可以
				3.当指针不初始化时,就会拥有一个不确定的值,很危险。
*/

//2.20
#include<iostream>
using namespace std;
int main()
{
	int i = 42;
	int*p1 = &i;
	*p1 = *p1**p1;	//访问p1对象的平方赋值给p1
	cout << *p1 << endl;
	cout << 42 * 42;
	return 0;
}

//2.21
#include<iostream>
using namespace std;
int main()
{
	int i = 1;
	//double *dp = &i;		//错误,存在类型不同
	//int *ip = i;		//错误,指向的是一个地址,而不是对象
	int *p = &i;		//正确,指向类型相同的地址
	return 0;
}

//2.22,2.23
#include<iostream>
using namespace std;
int main()
{
	int a1 = 1, a0 = 0;
	int *p1 = &a1;
	int *p0 = &a0;
	int *np = nullptr;
	int *nnp;
	if (a1)			//非0为true
		cout << "a1 is true" << endl;
	else
		cout << "a1 is false" << endl;
	if (a0)			//0为false
		cout << "a0 is true" << endl;
	else
		cout << "a0 is false" << endl;
	if (p0)			//检验p0的地址
		cout << "p0 is true" << endl;
	else
		cout << "p0 is false" << endl;
	if (*p0)			//检验p0的值,因为为空指针则属于false
		cout << "*p0 is true" << endl;
	else
		cout << "*p0 is false" << endl;
	if (p1)			//检验p1的地址
		cout << "p1 is true" << endl;
	else
		cout << "p1 is false" << endl;
	if (*p1)			//检验p1的值
		cout << "*p1 is true" << endl;
	else
		cout << "*p1 is false" << endl;
	if (np)			//检验np的地址,因为为空指针则属于false
		cout << "np is true" << endl;
	else
		cout << "np is false" << endl;
	//if (*np)		//检验np的值,因为无值直接出错
	//	cout << "*np" << endl;		//引发了异常: 读取访问权限冲突。np 是 nullptr

	//if(nnp)
	//用于检测该对象是否合法
	//if(*nnp)
	return 0;
}

//2.24
/*
void*类型可以指向任意类型的指针
*/
#include<iostream>
using namespace std;
int main()
{
	int i = 1;
	void *p1 = &i;
	cout <<"未赋值地址:"<< p1 << endl;
	p1 = (void*)020;		//可以赋值,类型为void*,十六进制
	cout <<"赋值后地址:"<< p1 << endl;
	//cout << *p1 << endl;		//不能输出不确定类型量
	void *p2 = nullptr;
	if (!p2)
		cout << "void*类型能指定为空" << endl;
	p2 = p1;
	cout << "p1赋值给p2:"<<p2<<endl;
	//void *p2 = *p1;				//非法的间接寻址
	return 0;
}

//2.3.3节思考
#include<iostream>
using namespace std;
int main()
{
	int i = 0x10;
	int &r = i;
	cout <<"r的地址:"<< &r << endl;
	cout <<"r的值:"<< r << endl;
	int *p = &i;
	int &r2 = *p;		//*p被看作obj
	//int &rrr = p;		//而引用不能赋给指针地址
	cout <<"r2的值:"<< r2 << endl;	
	//禁止套娃
	int **pp = &p;
	cout << "*p地址:" << p << endl;
	cout << "p的值:" << *p << endl;
	cout << "**pp地址:" << pp << endl;
	cout << "*pp地址:" << *pp << endl;
	cout << "pp值:" << **pp << endl;

	int *&r3 = p;
	cout << "r3的地址:" << r3 << endl;
	cout << "r3的值:" << *r3 << endl;
	int **&r4 = pp;
	cout << "r4的值:" << r4 << endl;
	//int &*r4 = p;		//非法,没有这个写法
	return 0;
}
/*输出示例
r的地址:007CF8A8
r的值:16
r2的值:16
*p地址:007CF8A8
p的值:16
**pp地址:007CF890
*pp地址:007CF8A8
pp值:16
r3的地址:007CF8A8
r3的值:16
r4的值:007CF890
*/

//2.26
/*
const必须初始化
const不能改变值
*/

//2.4.1思考
#include<iostream>
using namespace std;
int main()
{
	const int c1 = 020;
	const int &cr1 = c1;
	cout <<"cr1的值:"<< cr1 << endl;
	//cr1 = 2;		//不可修改
	//c1 = 2;		//不可修改
	const int &cr2 = c1 * 2;
	cout << "cr2被const常量赋值:" << cr2 << endl;
	//int &r1 = c1;		//const不可被非const的对象赋值
	const int &cr3 = cr1 * 2;
	cout << "cr3被const引用赋值:" << cr3 << endl;

	const double i1 = 016.5;		//仍然是10进位制
	double i2 = 016.5;
	const int &cr4 = i1;		//警告,类型强制转换
	cout << "int的cr4被double16.5(const)赋值:" << cr4 << endl;

	const double *cp1 = &i1;
	//double *cp2 = &i1;			//const地址不能被非const*指向
	double *p2 = &i2;
	cout << "const*cp1被const地址i1赋值:" << *cp1 << endl;
	cout << "此时的cp1地址:" << cp1 << endl;
	cp1 = &i1;
	cout << "给const*地址赋值合法但不会被改变:"<<cp1 << endl;
	//*cp1 = 0.005;			
	//*cp1 = i1;		//const指针的值不能被赋值
	//double *p3 = &i1;		//非const指针不能连接const地址

	double i3 = 1008.6;
	double *p3 = &i3;
	cout << "i3地址:" << &i3 << endl;
	double *const *const pc1 = &p3;
	//double *const pc2 = &i1;		//*const不能指向const的地址
	 long double i4 = 3.1415;
	 cout << "i4地址:" << &i4 << endl;
	 cout << "p2地址:" << &p3 << endl;
	//**pc1 = &i3;		//*const不可修改地址
	 **pc1 = i3;		//地址不可变,可赋值
	
	 cout << "**pc1的地址:" << &pc1 << endl;
	 cout << "*pc1的地址:" << *pc1 << endl;
	 cout << "pc1的地址:" << pc1 << endl;
	 cout << "pc1的值:" << **pc1 << endl;

	return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值