`

const pointer 与 pointer to const variable 的区别

阅读更多

如果能分清  T const * prt ;

                 const T * prt;

                 T * const prt;

三者区别, 就可以不用往下看了

 

A const pointer essentially means you can’t change the pointer variable itself, but you can change the value it points to.

const pointer(常量指针) 表示这个指针是指向一个固定的地址,  不能一会指向这,一会指向那;

 

A pointer to const means you can change the pointer but not what it points to;

 

a pointer to const (指向常量的指针)表示指针指向的 变量是一个常量, 不能改变这个常量的针,  但却可以改变指针的值, 让它指向别的地方。

 

 

 

int a = 100;
int * const prt = &a;// 表示prt指针是 常量指针, 不能改它的值

//prt++; <- won't compile ,若想改变它, 编译的时候会报错

*prt = 250; //但是可以改变它指向的变量的值



 

int a = 100

int const * prt  = &a; //指向常量的指针,pointer to const variable

//也可以这么写

const int *prt = &a;  //指向常量的指针,pointer to const variable

//这两种写法区别不大, const int ,  int const 不区分前后, 与java中
//public static ,  static public 一样,不分前后

*prt = 200; //不能这么写,编译出错, 常量的值怎么可以改变呢

prt ++; //没关系, 指针的值可以随便改;

 

reference:

http://blog.voidnish.com/?p=37

http://www.allinterview.com/showanswers/36962.html

 

 

分享到:
评论

相关推荐

    c++中const 的各种用法

    const TYPE memberVariable; }; ``` 2. **示例**: ```cpp class A { public: const int nValue; // 成员常量 }; A obj(nValue); ``` #### 七、总结 `const`关键字在C++中有着广泛的应用,从简单的变量到...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Naming General Naming Rules File Names Type Names Variable Names Constant Names Function Names Namespace Names Enumerator Names Macro Names Exceptions to Naming Rules Comments Comment Style File ...

    stdafx.h代码

    // This source code is only intended as a supplement to the // Microsoft Foundation Classes Reference and related // electronic documentation provided with the library. // See these sources for ...

    无标题C++ssssss

    常量指针是一种指针,用于存储常量的地址,例如,const int* pointer = &variable;语法用于定义一个常量指针pointer,指向常量variable的地址。指针常量是一种常量,用于存储指针的地址,例如,int* const pointer =...

    cocos2d-x c++的iconv.rar

    A pointer to such an object can be used as an iconv_t. */ typedef struct { void* dummy1[28]; #if @USE_MBSTATE_T@ mbstate_t dummy2; #endif } iconv_allocation_t; /* Allocates descriptor for code ...

    Using LUA with Visual C++ (Introduction)

    The first one is the pointer to the LUA state, the second one is a pointer to a user-defined reader function, the third pointer is a user-defined value that the reader function will receive, and the ...

    Google C++ Style Guide_英文版.pdf

    ### Google C++ Style Guide ...- **Pointer and Reference Expressions:** Place asterisks and ampersands next to the type rather than the variable. - **Boolean Expressions:** Use logical AND (`&&`)...

    c语言指针参考学习到精通

    3. **指针初始化**:可以通过`pointer_name = &variable_name;`的方式将变量的地址赋值给指针变量。 #### 三、指针与数组的关系 在C语言中,指针和数组之间有着密切的联系。本部分将详细介绍如何使用指针访问数组...

    MFC显示PPT

    // Make sure there is a dispatch pointer for PowerPoint. if(m_ppt.m_lpDispatch == NULL) { // Display a message indicating that PowerPoint is not running. MessageBox("PowerPoint is not running.", ...

    C PROGRAMMING TUTORIAL

    - **Using `const`:** Keyword to declare a constant variable. - **Syntax:** `type const NAME = value;` - **Example:** `int const MAX = 100;` #### C Storage Classes Storage classes determine the ...

    react-使用styledcomponents和其它React组件创建灵活的组合UI组件

    4. **使用CSS伪类和变量**:`styled-components`允许使用CSS的伪类选择器(如`:hover`, `:active`, `:focus`)以及CSS变量(`--variable-name`),进一步增强样式控制。 5. **主题(Themes)**:`styled-components...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - ADD: Add TFlexPanel.InvalidateControl virtual method which calls from TFlexControl.Invalidate and can be overriden (it is possible now to catch all object invalidation calls). - FIX: The TFlexPanel....

    C++专有名词定义

    65. **常量指针(Const Pointer)**:指向常量的指针,不能通过它改变所指向的数据。 66. **常量引用(Const Reference)**:指向常量的引用。 67. **函数指针(Function Pointer)**:指向函数的指针,可以像普通...

    Essential C

    - **Reference Parameters**: Passing arguments by reference, allowing functions to modify the original variable directly. - **Const Qualifier**: Specifying that a parameter should not be modified ...

    c++出错提示英汉对照表

    64. **Reference to local variable 'xxx' after its lifetime** - 'xxx'生命周期结束后引用局部变量:在变量的作用域之外引用变量。确保只在变量的生命周期内引用变量。 65. **Size of array 'xxx' depends on ...

    Object Pascal 中英文对照语法参考

    - **常量声明(Constant Declaration)**:使用 `const` 关键字,如 `const pi = 3.14;`。 - **变量初始化(Variable Initialization)**:可以在声明时初始化,如 `var x: Integer = 5;`。 ### 4. 控制流程 - **...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    - **Scope and Storage Class:** Discusses variable scope and storage classes. - **Namespaces:** Explains namespaces and their use in organizing code. - **Pointer Types:** Describes pointers and their ...

    Google C++ International Standard.pdf

    7.12 Pointer to member conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 7.13 Function pointer conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

    c reference card

    - **常量 (Constant Value):** 使用 `const` 关键字定义只读变量。 - **外部变量声明 (External Variable Declaration):** 使用 `extern` 关键字声明外部变量。 - **静态局部变量 (Static Local Variable):** 使用 `...

Global site tag (gtag.js) - Google Analytics