使用c++的时候,经常会在 const int *p 和 int * const p这个地方迷惑。这里记录一下:

const int *p  = int const *p //这里const后面的为*p, 将他们视为一个整体,即 *p是const的,即p指针指向的内容是不可以修改的;但是p是可以修改的。

int* const p; //const 修饰的为p,即p是不可修改的,但是p指向的内容是可以修改的.

    int a = 1, b = 2;
const int c = 10;
const int* p = &a;
//*p = 10; //出错,不可修改指向的内容
a = 20; //可以通过别的手段修改,但是不能通过指针p修改
p = &b; //可以 int* const p2 = &a;
//p2 = &b; //出错,不能修改指针
*p2 = 100; //可以 //int* const p3 = &c; //错误,非常量指针不能指向常量,因为可以通过*p3修改内容,
//而c为常量,不能被修改。防止通过非常量指针修改常量
const int* p4 = &c;

如果*在const之后,则const就修饰的是 *p 表示指向的内容是const的; 如果 *在const之前,则const修饰的只是p,表示指针是const的,而内容可以改变。

const T* 和 T* const的更多相关文章

  1. construction const parameter问题 构造函数const引用参数问题

    工程在window下编译没有任何问题, 但是在linux(CentOS6)下编译就老是报错 C++ 编译器已升级到最新版 6.1.0 错误如下: In file included /bits/stl_ ...

  2. 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)'

    error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Tra ...

  3. 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, const 对象的引用

    [源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象,  const 指针和指向 const 对象的指针, con ...

  4. C++ const用法 尽可能使用const [转载]

    C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不变的.如果在编程中确实有某个值保持不变,就应该明确使用const,这样可以获得编译器的帮助. 1.c ...

  5. 【转】深入理解const char*p,char const*p,char *const p,const char **p,char const**p,char *const*p,char**const p

    一.可能的组合: (1)const char*p (2)char const*p (3)char *const p(4)const char **p (5)char const**p (6)char ...

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

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

  7. C++杂谈(一)const限定符与const指针

    const限定符 c++有了新的const关键字,用来定义常变量,可以替C语言中的#define.关于const限定符,有以下需要注意: 1.创建后值不再改变 2.作用范围在文件内有效 3.添加ext ...

  8. 对于这个函数const int func(const int& a) const声明中,三个const分别是什么意思?

    第一个const 函数的返回值类型是const. 这个const修饰没什么意义,你可以想象一下: 既然是函数的 返回值,而且是值传递的形式,是否const有什么意义.如果指针(引用)传递,怎表示返回值 ...

  9. 指向const的指针和const指针

    1.指向const的指针:const int *p 或 int const *p 解释:p是一个指针,指向const int类型的常量:指针指向的内容为常量,因此不能改变*p的值,但指针p可以改变,指 ...

  10. const int * pi/int * const pi的区别

    前面有一篇文章:数组名就是常量指针 参考文章:http://blog.pfan.cn/whyhappy/5164.html const int * pi .int const * pi与int *   ...

随机推荐

  1. 为gridview的每个单元格添加鼠标悬停提示文本(tooltip)

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    {        for (int i ...

  2. CentOS6.5下安装Cloudstack

    个人记录: 使用yum源安装,地址:http://mirrors.163.com/.help/CentOS6-Base-163.repo 后续待进行

  3. CentOS6.5下安装Nexus

    一.环境准备 (1) CentOS6.5 (2) nexus-latest-bundle.tar.gz 下载地址:http://www.sonatype.org/nexus/archived/ 二.安 ...

  4. zoj3228 Searching the String AC自动机查询目标串中模式串出现次数(分可覆盖,不可覆盖两种情况)

    /** 题目:zoj3228 Searching the String 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=34 ...

  5. KBEngine.executeRawDatabaseCommand使用

    先贴一段官方的API介绍: def executeRawDatabaseCommand( command, callback, threadID, dbInterfaceName ): 功能说明: 这 ...

  6. 自然语言交流系统 phxnet团队 创新实训 项目博客 (一)

    2D文字聊天界面大致预期实现文字输入.发送消息.接收消息.你可以通过点击按钮让机器人开启聊天模式或者学习模式.又或是进入3D语音聊天界面或者退出. 目背景 (1) 开发动机的形态 随着科技的进步与生活 ...

  7. 获取FirefoxProfile配置文件以及使用方法介绍

    使用默认方式构建的(WebDriver)FirefoxDriver实例: WebDriver driver = new FirefoxDriver(); 这种方式下,打开的Firefox浏览器将是不带 ...

  8. Latex的各种帽子

    \hat{A} \widehat{A} \tilde{A} \widetilde{A} \overline{A} \underline{A} \overbrace{A} \underbrace{A} ...

  9. Spring Annotation是怎么工作的?

    最近刚好看了下注解,虽然明白了注解的作用原理,但是仍然不明白Spring中的注解是如何工作的. 占座用,留待后续. 先来两个链接吧 https://dzone.com/articles/spring- ...

  10. R语言绘图边框

    在R语言中, 绘图边框一共有3个区域: device region : figure region : plot region   : 在描述不同区域大小的时候,有对应的不同参数: din : 返回d ...