区分const指针

 

Read it backwards...

  • int* - pointer to int
  • int const * - pointer to const int
  • int * const - const pointer to int
  • int const * const - const pointer to const int

Now the first const can be on either side of the type so:

  • const int * == int const *
  • const int * const == int const * const

If you want to go really crazy you can do things like this:

  • int ** - pointer to pointer to int
  • int ** const - a const pointer to a pointer to an int
  • int * const * - a pointer to a const pointer to an int
  • int const ** - a pointer to a pointer to a const int
  • int * const * const - a const pointer to a const pointer to an int
  • ...

And to make sure we are clear on the meaning of const

const int* foo;
int *const bar; //note, you actually need to set the pointer
                //here because you can‘t change it later ;)

foo is a variable pointer to a constant int. This lets you change what you point to but not the value that you point to. Most often this is seen with cstrings where you have a pointer to a const char. You may change which string you point to but you can‘t change the content of these strings. This is important when the string itself is in the data segment of a program and shouldn‘t be changed.

bar is a const or fixed pointer to a value that can be changed. This is like a reference without the extra syntactic sugar. Because of this fact, usually you would use a reference where you would use a T* const pointer unless you need to allow null pointers.

时间: 2024-08-07 08:35:56

区分const指针的相关文章

速记const 指针与指向const的指针

指向const的指针,它的意思是指针指向的内容是不能被修改的.它有两种写法. ` const int* p; (推荐) int const* p;` 再说const指针,它的意思是指针本身的值是不能被修改的.它只有一种写法 int* const p=一个地址; (因为指针本身的值是不能被修改的所以它必须被初始化) 这两种情况很难分清,你只需注意在const后面的是*符号还是变量,*在后说明,const的是指针指向的对象,变量在后说明const的是指针本身 版权声明:本文为博主原创文章,未经博主允

const指针

const指针有三种形式: 1)const出现在*左边 2)const出现在*右边 3)   const出现在*两侧 const出现在*左边表示被指对象是常量:出现在右边表示指针自身是常量:出现在两侧表示被指对象和指针本身都是常量. 另外, int const *p与 const int *p是等价的,即只有当const与*相对位置发生变化才有区别. const指针,布布扣,bubuko.com

const指针和指向const的指针

int *const p=&a; 这是const指针,这种指针必须在定义时就给出它所指向的地址,否则会error:uninitialized const 'p'.const指针的指针本身是const类型,所以不能修改它所指向的地址,但可以修改它所指向的值. const int *p; 这是指向const对象的指针,可以修改指向的地址,但不能通过这种指针来修改它所指向的值,即使它所指向的值不是const类型的. const int *const p=&a; 这是指向const类型的const

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

[源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象,  const 指针和指向 const 对象的指针, const 对象的引用 作者:webabcd 介绍不可或缺 Windows Native 之 C++ this 指针 对象数组 对象和指针 const 对象 const 指针和指向 const 对象的指针 const 对象的引用 示例1.CppEmployee 类CppEmployee.h #pragma

const指针总结

const 总结: 假设keywordconst出如今星号左边.表示被指物是常量:即不能通过指针改动变量的值. 假设keywordconst出如今星号右边,表示指针自身是常量:即不能改变指针的指向. 假设被指物是常量,会有两种形式: int a=3,b=4; const int* p=&a; int const *p=&a; 这两种形式等效. 详细举例见:const指针

指向const对象的指针和const指针

const char *p1;/指向const对象的指针 char const *p2;//同上 char *const p3;//const 指针 区别:const后面是什么就限定什么,比如char const *p 就是限定(*p),(*p)就是p指向的那段内存不能变,p的值可以改变,如果是char* const p就是限定p指针的值. 1. 指向const对象的指针,适合做函数形参,保证指向对象不被修改 1.1 p1指向的值不可改变,指向const对象,但是p1可以被修改指向非const对

深入学习 const指针,const引用

指针和引用的区别: 1.指针可以为空,引用不可以为空. 2.指针初始化后可以重新指向新对象,引用初始化以为不可以重新绑定新对象, 3.指针可以在初始化时赋值,可以初始化以后通过赋值运算符(=)赋值:引用只能在初始化时赋值. 4.指针是个实体(占内存4byte),引用只是个别名(不占内存) 5.指针sizeof是4byte,引用sizeof是绑定对象的大小. 6.指针是类型安全,引用是类型安全的. const对于指针和引用的区别: int a = 1: int b = 1: const int *

const 指针的三种使用方式

///////////////////////const 指针的三种状态///////////////////// 注意:const 的前后顺序 const 在类型之前 ---可以修改指针包含的地址,不能修改指针指向的值 const 在变量之前类型之后 ---可以修改指针的指向值,不能修改指针地址 // 1.指针指向的数据为常量,不能修改,但是可以修改指针包含的地址 /*int HoursInDay = 24;const int* pInteger = &HoursInDay; cout<&

【c++基础】const、const指针、const引用

一.const常量 声明时必须同时初始化(和“引用”一样) 二.const指针 三.const引用 引用本身和引用的对象都是const对象,可以用字面值来赋给const引用(普通引用则不行) 1 const B=1024; 2 const refB=B; //const变量的声明和初始化 3 4 const int &r=42;//字面值给const赋值 5 int &r2=42;//错 [c++基础]const.const指针.const引用