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 ...
随机推荐
- Flex博客
http://blog.csdn.net/xingfeng0501/article/details/7533426
- mysql性能优化(二)
###> mysql中有一个explain 命令可以用来分析select 语句的运行效果,例如explain可以获得select语句使用的索引情况.排序的情况等等.除此以外,explain 的e ...
- Linux根文件系统的制作
转载:http://www.cnblogs.com/hnrainll/archive/2011/06/09/2076655.html 1. 根文件系统 文件系统是包括在一个磁盘(包括光盘.软盘.闪盘及 ...
- javascript中的原型和闭包
定义 //闭包测试 function bbTest() { var local = "这里是本地变量"; //闭包会扩大局部变量的作用域,具备变量一致会存活到函数之外,在函数之外可 ...
- sed命令用法详解
sed命令用法 sed是一种流编辑器,它是文本处理中非常有用的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为『模式空间』(pattern space) ...
- 关于c#调用c/c++ dll遇到的问题总结
前段时间公司做了个winform程序,需要调用c 的dll去读取卡号的程序,期间遇到些问题,下面来分享下 一.dll路径问题 相信很多开发者都会遇到这个问题,我总结了下我现在有3总方式去解决这个问题: ...
- viewflipper动画切换屏幕
整个项目的 package com.example.viewflipper; import android.R.integer; import android.app.Activity; import ...
- struts2+jquery 实现ajax登陆
一.新建一个web项目:test,配置好struts2的环境(详细配置见:http://www.cnblogs.com/wuweidu/p/3841297.html) 导入Jquery的j ...
- centos6.5 iptables实现端口转发
将本地接口IP 61.144.a.b 的3389端口 转发到 116.6.c.d的3389 (主要访问到61.144.a.b的3389端口,就会跳转到116.6.c.d的3389) [步骤] ...
- POJ 2516 Minimum Cost 最小费用流
题目: 给出n*kk的矩阵,格子a[i][k]表示第i个客户需要第k种货物a[i][k]单位. 给出m*kk的矩阵,格子b[j][k]表示第j个供应商可以提供第k种货物b[j][k]单位. 再给出k个 ...