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 * 的区别的更多相关文章

  1. const int * p 和 int const * p 和 int * const p 的区别

    首先注意,const int * p 和int const *p 是一样的,并且不管是不是*p,即使const int i和int const i也是一样的,所以我们接下来只讨论int const * ...

  2. c语言检测文件是否存在int __cdecl access(const char *, int);

    最近写代码,遇到很多地方需要判断文件是否存在的.网上的方法也是千奇百怪,“百家争鸣”. fopen方式打开的比较多见,也有其他各种方式判断文件是否存在的,由于其他方法与本文无关,所以不打算提及. 笔者 ...

  3. 【转】int const A::func()和int A::func() const

    int const A::func() { return 0; }int A::func() const { return 0; } 上面的代码是合法的,其中A::func成员函数[只能在成员函数后面 ...

  4. const vector<int> 和 vector<const int>问题讨论

    1.const vector <int> vec(10) —— 与const int a[10]是一回事,意思是vec只有10个元素,不能增加了,里面的元素也是不能变化的 vector&l ...

  5. 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 ...

  6. const&nbsp;int&nbsp;*pi与int&amp;nbs…

    此质料是摘要:<<彻底搞定C 指针 >>,自己感觉比较有价值,现与大家分享. 1. 从const int i 说起 你知道我们声明一个变量时象这样int i :这个i是可能在它 ...

  7. int *p,cons int *p,int const *p,int * const p,const int * const p,int const * const p的差别

     加有constkeyword的几种情况的辨析 const修饰的代码 含义(特点) 等价性 int *p = &num; 1.       能够读自己 2.       能够通过*p改自己 ...

  8. int *const 与const int *问题

    自己一直就不太清楚int *const与const int*之间的差别,总是弄混,今天势必拿一个程序验证一下. 一个指针是有两个属性的,一个是它指向的地方,一个是它指向地方上的内容.两者的差别也在此. ...

  9. c++中typedef、define、const、inline之间的区别

    1.typedef和#define的区别 typedef int* pInt; , b = ; const pInt p1 = &a; //p1是常量指针 pInt const p2 = &a ...

随机推荐

  1. linux 安装svn最新版本

    一.安装svn yum install -y subversion-* 结果为Complete就为正确 查看svn版本号 svnserve --version 1.6.11版本 mkdir /opt/ ...

  2. memcachedb-持久化存储的缓存系统

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached ...

  3. 【python,排序】几种常用的排序算法,使用python实现

    1. 选择排序 -- -- def selectSort(l): for i in range(len(l)): j = i + 1 t_min = l[i] loc_min = i for j in ...

  4. iOS - UI - UITableView

    1.UITableView 表格视图 服从数据源 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSIn ...

  5. 《算法导论》习题解答 Chapter 22.1-4(去除重边)

    思路:重开一个新图,按着邻接列表的顺序从上到下遍历,每遍历一行链表前,清空visited数组,如果没有访问过这个元素,则加入新图,如果已经访问过了(重边),则不动. 伪代码: 复杂度:O(V+E) f ...

  6. open source e-business software - prestashop

    https://www.prestashop.com/en/system-requirements

  7. 【递归】数字三角形 简单dp

    [递归]数字三角形 题目描述 对于大多数人来说,“我们是这么的正常,因此也就这么的平庸.”而天才总是与众不同的,所以当邪狼问修罗王:“老大,你蹲在那儿一动不动看了有半个小时了,蚂蚁有那么好看吗?” 修 ...

  8. codeforces 613B B. Skills(枚举+二分+贪心)

    题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  9. python字符串反转

    最一般的想法就是将字符串先转换成列表,倒置列表,再将列表转换为字符串 s = 'Hello world' l = list(s) l.reverse() python ''.join(l) 而pyth ...

  10. 第一个项目--用bootstrap实现美工设计的首页

    主要介绍在首页实现中用到bootstrap实现效果的地方. 实现如下的效果: <li> <div role="group" style="padding ...