Const *ptr ptr

1. const int *ptr = NULL; <=> int const *ptr = NULL;

1) 表示指向符号常量的指针变量,指针变量本身并非const所以可以指向其他变量。

2) const 的作用可以看作是“禁止通过*ptr"来改变被ptr指向的变量的值,但被指向的变量 其本身能否改变需要看其定义

eg: const int i = 1, int j = 2;

ptr = &i; // ok

*ptr = 27 // error

i = 2 // error i is const

ptr = &j; // ok

*ptr = 28; // error

j = 3; // ok

2. const int *ptr vs int * const ptr;

1) 要区分以上两种形式重点看const与*的相对位置:

const在*之前表示”指向符号常量的指针“

const在*之后表示”指向int类型变量的常量指针

注意:若声明成 int * const ptr = NULL;则ptr的值不可改即不可再指向其它变量,

但可以通过*ptr修改其指向的变量(如果指向了的话)。

2)识记技巧

看const修饰的是*ptr还是ptr?

当 const 在 * 之前可以认为 const修饰的是 *ptr,此时*ptr的”值“不可变,ptr的值可变

当 const 在 * 之后可以认为 const修饰的是 ptr,此时ptr的值不可变,*ptr的值可变

const修饰谁,谁的值就不可再变。

3. const int * const ptr;

1) 表示”指向int型符号常量的常量指针“

2) const 即修饰了 *ptr 表示不可通过*ptr修改其指向变量的值

又修饰了 ptr 表示ptr中的值不可再修改即不能再指向其他变量

时间: 2024-07-28 16:09:00

Const *ptr ptr的相关文章

Why am I able to change the contents of const char *ptr?

http://stackoverflow.com/questions/3228664/why-am-i-able-to-change-the-contents-of-const-char-ptr I passed a pointer ptr to a function whose prototype takes it as const. foo( const char *str ); Which according to my understanding means that it will n

【C++】智能指针类和OpenCV的Ptr模板类

智能指针类 引用计数 智能指针(smart pointer)的一种通用实现技术是使用引用计数(reference count).智能指针类将一个计数器与类指向的对象相关联,引用计数跟踪该类有多少个对象的指针指向同一对象.引用计数为0时,删除对象. 其基本使用规则是: 每次创建类的新对象时,初始化指针并将引用计数置为1.当对象作为另一对象的副本而创建时,复制构造函数复制指针并增加与之相应的引用计数的值.对一个对象进行赋值时,赋值操作符减少左操作数所指对象的引用计数的值(如果引用计数减至0,则删除对

opencv中ptr的使用

#include <QCoreApplication> #include<stdio.h> #include<opencv2/highgui/highgui.hpp> #include<opencv2/core/core.hpp> using namespace cv; void colorReduce(Mat &image,int div =64){ int nl = image.rows; int nc =image.cols*image.cha

const、volatile、mutable的用法

原文:http://dev.yesky.com/393/3007393.shtml const修饰普通变量和指针 const修饰变量,一般有两种写法: const TYPE value; TYPE const value; 这两种写法在本质上是一样的.它的含义是:const修饰的类型为TYPE的变量value是不可变的.对于一个非指针的类型TYPE,无论怎么写,都是一个含义,即value值不可变. 例如: const int nValue:    //nValue是const int const

C++ Primer 学习笔记_25_类与数据抽象(11)--const 用法小结、static与const以及static const(const static)

一.const 用法总结 1.可以对const 的用法做个小总结: const int n = 100;  //定义常量 const Test t(10); const int & ref = n;   //const引用 int& ref = n;  //Error [const与指针] const int* p; //const出现在*前面,表示*p是常量 (*p = 200; //Error) int * const p2;  //const出现在*后面,表示p2是常量 (p2 =

C/C++里的const(2)

对于如下几个语句,哪些定义相同?哪些定义不同?哪些数据可修改?哪些数据不可修改呢? 1 const int a; 2 int const a; 3 const int *a; 4 int *const a; 5 int const *const a; 在C/C++中,const是一个数据类型修饰符,常见的还有short.long.unsigned.static.extern等,定义变量的方式采用: (修饰符+数据类型) 变量名称 注意:如果有修饰符,则修饰符和数据类型的位置不影响变量的定义,比如

const的用法

在other.cpp中 #include<iostream> using namespace std; #const int global=5;  //加入const 会让global原来的外部的链接型转为内部链接性. extern const int global=5; void print(){ cout << global << endl; } 在main.cpp中 #include<iostream> using namespace std; ext

c中常用的关键字static const volatile

在C语言中,关键字static有三个明显的作用:1). 在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变.2). 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所用函数访问,但不能被模块外其它函数访问.它是一个本地的全局变量.3). 在模块内,一个被声明为静态的函数只可被这一模块内的其它函数调用.那就是,这个函数被限制在声明它的模块的本地范围内使用.大多数应试者能正确回答第一部分,一部分能正确回答第二部分,同是很少的人能懂得第三部分.这是一个应试者的严重的缺点,因

c++ const小结

1.如果const出现在*左边,表示被指物是常量(被指物内容不能修改,但指针可以指向其他对象):如果const出现在*右边,表示指针本身是常量(指针不能再指向其他对象) int k = 9;const int* ptr = &k; const * int ptr = &k;int const* ptr = &k;   //这三种方式在vc++6.0中都是可以通过编译的,都代表变量 k的值不能被修改,但指针ptr可以指向其他对象int* const ptr = &k;   /