使用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. thrift学习之一-------介绍

    thrift 官网:  http://thrift.apache.org/ Thrift是一个软件框架,用于支持可扩展的跨语言服务的开发,它无缝的与C++,Java,Python,PHP,Ruby,E ...

  2. /usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory

    cc -DDEBUG -mtune=core2 -O2 \ -onvideo nvideo.c \ -I/usr/include/atk-1.0 \ -I/usr/include/cairo \ -I ...

  3. ruby send respond_to

    http://ruby-metaprogramming.rubylearning.com/html/ruby_metaprogramming_2.html http://galeki.is-progr ...

  4. PHP7新特性的介绍

    关于PHP 20年的发展历史: 迄今为止最流行的WEB开发语言: 超过82%的网站都会使用PHP作为他们的服务端开发语言: 新特性介绍 PHP NG – Zend Engine 3 抽象语法树 64位 ...

  5. Sphinx速成指南

    目录 1. Sphinx简介 1.1. 什么是全文检索 1.2. 介绍 1.3. Sphinx的特性 2. Sphinx安装(For MySQL) 2.1. Windows下安装 2.2. Linux ...

  6. r语言与dataframe

    什么是DataFrame 引用 r-tutor上的定义: DataFrame 是一个表格或者类似二维数组的结构,它的各行表示一个实例,各列表示一个变量. 没错,DataFrame就是类似于Excel表 ...

  7. C语言中带参数的宏

    带参数的宏定义有如下的格式: [#define 指令----带参数的宏] #define 标识符(x1,x2,……,xn) 其中  x1,x2,……xn是标志符(宏的参数) 注意:在宏的名字和括号之间 ...

  8. .net开发遇到的一个问题

    之前项目有个entity是写在Entity层的,相关的配置项也写死在程序里了,而且还是个static的配置,后来有了新需求,上峰指示要从CMS读取配置内容,大概是要在BLL实现,BLL依赖IBLL的I ...

  9. e653. 写入段落文本

    In order to change the font of the text, you need to supply an attributed string to the LineBreakMea ...

  10. CentOS系统命令

    系统命令 yum命令 yum makecache yum 生成缓存 yum list installed mysql* 查看有没有安装过*包 rpm -qa | grep mysql* 查看有没有安装 ...