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 ...
随机推荐
- 用systemtap对sysbench IO测试结果的分析1
http://www.actionsky.com/docs/archives/171 2016年5月6日 黄炎 近期在一些简单的sysbench IO测试中, 遇到了一些不合常识的测试结果. 从结 ...
- 引入第三方库错误Undefined symbols for architecture i386: _OBJC_CLASS_$的解决方案
引起标题上所导致的错误是因为你的第三方库没有放入到Compile Sources里面去. 需要到你项目的Targets>>Build Phases>>Compile Sourc ...
- 获取div相对文档的位置
获取div相对文档的位置,两个方法 经测试 document.getElementById("btn").getBoundingClientRect() 在IE6下有2像素的bug ...
- HTML与Servlet
1.什么是servlet Servlet 是在服务器上运行的小程序.一个 Servlet 就是 Java 编程语言中的一个类,它被用来扩展服务器的性能,服务器上驻留着可以通过“请求-响应”编程模型来访 ...
- A+B Coming
Problem Description Many classmates said to me that A+B is must needs.If you can’t AC this problem, ...
- XACML-PolicySet与request结构简介
本文由@呆代待殆原创,转载请注明出处. 一.PolicySet的结构 PolicySet 的基本嵌套结构如上图所示,下面让我们一个一个来说明. PolicySet:XACML策略架构的顶层元素,由Po ...
- 2015 FVDI V6.3 Software Free Download
2015 FVDI with software USB dongle has newly upgraded to V6.3. Here software upgrade list: ABRITES C ...
- python使用rrdtool时 argument 0 must be string的问题
在updatev rrdfile时, ret = rrdtool.updatev(filename, ds) 报了argument 0 must be string的异常,经查是因为python 的r ...
- 单例模式——Singleton
模式分类: 从目的来看: 1.创建型(Creational)模式:负责对象创建. 2.结构型(Structural)模式:处理类于对象间的组合. 3.行为型(Behavioral)模式:类与对象交互中 ...
- Spring MVC 学习笔记(整理)
SpringMVC学习 1.概述 Spring MVC是一种基于Java实现MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行解耦,基于请求-响应模型帮助我们 ...