欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 明星 > day13

day13

2025/2/6 20:22:20 来源:https://blog.csdn.net/weixin_64953311/article/details/143572009  浏览:    关键词:day13

目录

1 空指针和野指针

2 const 修饰符

2.1 指针常量

2.2 常量指针

2.3 常量指针常量


1 空指针和野指针

1)空指针

不能对空指针进行解引用 

2) 野指针

这个地址里并不知道有什么,谁也不知道就叫做野指针 

2 const 修饰符

2.1 指针常量


#include <iostream>using namespace std;
/*
const 和 指针的关系
*/
int main()
{int a = 1;int b = 2;//指针常量//指针的值是一个常量(从左往右读,指针常量)int* const p = &a; //p的值是不能改变的//p=&b; 错误*p = 6;cout << "a = " << a << endl;return 0;
}

2.2 常量指针

#include <iostream>using namespace std;
/*
const 和 指针的关系
*/
int main()
{int a = 1;int b = 2;//常量指针//指向常量的指针const int* p = &a;// *p = 6; 错误p = &b;cout << "p = " << *p << endl;return 0;
}

2.3 常量指针常量

#include <iostream>using namespace std;int main()
{int a = 1;int b = 2;//常量指针常量const int* const p = &a;//*p = 6; 错误//p = &b; 错误return 0;
}
指针常量type* const指针值是一个常量指针无法被赋值
常量指针const type*指向常量的指针指针解引用后无法被赋值
常量指针常量const type* const指针值和指向指针的值都是常量指针和解引用都无法被赋值

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com