int * const 与 const int * 的区别
type * const 与 const type * 是在C/C++编程中特别容易混淆的两个知识点,现在就以 int * const 和 const int * 为例来简略介绍一下这两者之间的区别。
1.int * const 讲解
int a = 20;
int * const b = &a;
b代表一个指向a变量存储空间的int *常量指针,由于b是一个常量指针,因此其指针值无法改变,亦即无法指向其他的存储空间,但其指向的存储空间的值可以通过 *b = newValue / a = newValue 改变。
示例代码如下:
- #include <iostream>
- using namespace std;
- int main()
- {
- int a = ;
- int * const b = &a;
- cout << *b << endl;
- *b = ;
- cout << *b << endl;
- int c = ;
- //b = &c; error
- cout << *b << endl;
- return ;
- }
2.const int * 讲解
int a = 20;
const int * b = &a;
b代表一个指向 const int 存储空间的指针,b所指向的存储空间的数值不可以通过 *b = newValue 改变,但可以通过 a = newValue改变,b 也可以指向其他存储空间。
示例代码如下:
- #include <iostream>
- using namespace std;
- int main()
- {
- int a = ;
- const int * b = &a;
- cout << *b << endl; // *b = 20
- //*b = 20; error
- a = ;
- cout << *b << endl; // *b = 30
- const int d = ;
- //int *e = &d; error
- const int * e = &d;
- cout << *e << endl; // *e = 55;
- //d = 20; error
- //*e = 30; error
- return ;
- }
int * const 与 const int * 的区别的更多相关文章
- const int * p 和 int const * p 和 int * const p 的区别
首先注意,const int * p 和int const *p 是一样的,并且不管是不是*p,即使const int i和int const i也是一样的,所以我们接下来只讨论int const * ...
- c语言检测文件是否存在int __cdecl access(const char *, int);
最近写代码,遇到很多地方需要判断文件是否存在的.网上的方法也是千奇百怪,“百家争鸣”. fopen方式打开的比较多见,也有其他各种方式判断文件是否存在的,由于其他方法与本文无关,所以不打算提及. 笔者 ...
- 【转】int const A::func()和int A::func() const
int const A::func() { return 0; }int A::func() const { return 0; } 上面的代码是合法的,其中A::func成员函数[只能在成员函数后面 ...
- const vector<int> 和 vector<const int>问题讨论
1.const vector <int> vec(10) —— 与const int a[10]是一回事,意思是vec只有10个元素,不能增加了,里面的元素也是不能变化的 vector&l ...
- VS2013 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array<int>::operator [](int)”只是在返回类型上不同
1,VS2013 错误 1 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array ...
- const int *pi与int&nbs…
此质料是摘要:<<彻底搞定C 指针 >>,自己感觉比较有价值,现与大家分享. 1. 从const int i 说起 你知道我们声明一个变量时象这样int i :这个i是可能在它 ...
- int *p,cons int *p,int const *p,int * const p,const int * const p,int const * const p的差别
加有constkeyword的几种情况的辨析 const修饰的代码 含义(特点) 等价性 int *p = # 1. 能够读自己 2. 能够通过*p改自己 ...
- int *const 与const int *问题
自己一直就不太清楚int *const与const int*之间的差别,总是弄混,今天势必拿一个程序验证一下. 一个指针是有两个属性的,一个是它指向的地方,一个是它指向地方上的内容.两者的差别也在此. ...
- c++中typedef、define、const、inline之间的区别
1.typedef和#define的区别 typedef int* pInt; , b = ; const pInt p1 = &a; //p1是常量指针 pInt const p2 = &a ...
随机推荐
- 关于CQRS(老外经典好文)
CQRS means Command Query Responsibility Segregation. Many people think that CQRS is an entire archit ...
- mysql优化之连接优化(open-files-limit与table_open_cache)
MySQL打开的文件描述符限制 Can't open file: '.\test\mytable.frm' (errno: 24) OS error code : Too many open file ...
- -force_load使用办法
项目中用到Three20框架,同时也使用了讯飞语音的framework,编译时出现类似如下冲突提示信息: ld: duplicate symbol _OBJC_CLASS_$_AdLinks in / ...
- Google 推出全新的两步验证机制
近日 Google 在官方的 Apps Updates 博客公布了全新的两步验证功能--Google 提示,新的功能通过与 Google App 联动,进一步将验证确认工作缩减到仅有两步,同时支持 A ...
- MUI功能列表
打开App引导页面 - http://www.bcty365.com/content-146-4970-1.html 页面传值 - http://www.bcty365.com/content-1 ...
- centos下编译安装mysql5.6
CentOS 6.4下编译安装MySQL 5.6.14 参考:http://www.cnblogs.com/xiongpq/p/3384681.html 概述: CentOS 6.4下通过yum安装的 ...
- 代码片段--Makefile之大型工程项目子目录Makefile的一种通用写法
转载:http://blog.csdn.net/mo_hui123456/article/details/8929615 管理Linux环境下的C/C++大型项目,如果有一个智能的Build Syst ...
- 轻松绕过极域电子教室、和教师控制 Say GoodBye
注意:以下博文(包括但不限于汉字.英文.阿拉伯数字 .图片.影像,以及前述之各种任意组合等等)均为随意敲击键盘所出,用于检验本人电脑键盘录入.屏幕显示的机械.光电性能,并不代表本人观点.如需要详查请直 ...
- UITabBar实现自定义背景及UITabBarItem自定义图片和字体
UITabBarItem *firstItem = [[UITabBarItem alloc]initWithTitle:]; //设置字体颜色(后面设置字体状态)(UITextAttributeTe ...
- 【trim()】去掉字符串开头和结尾的空格,防止不必要的空格导致的错误。
去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String a=" abc"; Strin ...