const pointer 和pointer const

     常量指针:cosnt pointer 所指向的地址上的数据是常量,所谓的常量指针,即const关键字,在*之前,如:const char *p。他的一般格式:const 数据类型 *变量名 ,也可以:数据类型 const *p;

    指针常量:pointer const 所指向的地址是常量,所谓的指针常量,即*在关键字const之前,如:* const char p。他的一般格式:数据类型 *  const   变量名


   When we look at pointer declarations, reading from right-to-left gives a better idea about what the pointer actually points to.
 
  Consider const int * p; if we read right to left: p -> * -> int -> const. i.e. 'p is a pointer to an integer constant' rather than 'constant integer pointer' (if we read left to right) which doesn't give an idea if the pointer is constant or the value it is pointing to.
 
  So reading it right to left gives clear idea that in the above declaration, p can be modified (like p++) but the value p is pointing to (*p) cannot be modified as it is pointing to a constant.
 
  Let us consider another example int * const p;. Again let us use the same right-to-left trick, p -> const ->* -> int. i.e. p is a constant pointer to an integer. In this case, p cannot be modified (like p++) but the value it is pointing to can be modified (like *p = 2).
 
Finally in const int * const p; 'p is a const pointer to an integer constant' by using the same RTL trick.


    常量指针和指针常量的区分方法:

    区分方法:我们从左到右,来区分指针常量和常量指针的区别

   如:const char *p,我们从左往右都p>*>char>const,p是一个指向常量的指针而不是常量指针(从左往右读),所以p的地址能够改变,但是p指向的地址能够改变即不能通过p来改变值,但是我们能够给p重新指向地址

  void main()
{
    char *ch={"A"};
    char const *p={"hello world"};
    p=ch;
    printf("%s\n",p);
}

 如:char * const p ,我们从左到右p>const>*>char是一个常量指针,所以p不能被改变,但是我们能够通过p进行对刚才地址数据的更改

void main()

{

     char str[6]={"hello"};

     char * const p=str;

     *p='A'

     printf("%c\n",*p);

}

//输出A

但是

void main()

{

     char *str={"hello"};

     char * const p=str;

     *p='A'

     printf("%c\n",*p);

}//不会打印

错误:原因自己找去


### C++ 中 `auto` `const` 关键字的用法及区别 #### 定义与基本概念 在现代C++编程中,`auto``const`关键字扮演着重要角色。`auto`用于自动类型推导,简化变量声明;而`const`则用来定义不可修改的数据成员或函数参数。 #### 自动类型推导中的顶层常量性底层常量性 当涉及到`auto`时,需要注意区分顶层(const qualifier applied directly to the variable itself)底层(const qualifier applies to what a pointer or reference refers to)常量性的不同处理方式[^3]: - **顶层const**: 当初始值是一个顶层`const`时,`auto`推断出来的类型将不是顶层`const`类型的。例如: ```cpp const int cIntVal = 12; auto val = cIntVal; // 'val' 的类型为 int 而非 const int ``` - **底层const**: 对于指针或引用来说,如果所指向的对象是`const`,那么这种情况下`const`属于底层特性。此时使用`auto`会保留这些属性: ```cpp const int *pConstIntPtr = nullptr; auto pAutoPtr = pConstIntPtr; // 'pAutoPtr' 类型为 const int* ``` #### 结合使用的场景分析 为了更好地理解两者如何一起工作,在某些特定上下文中可以观察到它们的行为差异。比如对于返回临时对象的方法调用表达式而言,通过添加额外的`&`或者`&&`修饰符来指定左值/右值引用形式能够影响最终的结果类型。 另外值得注意的是,在涉及模板元编程以及泛型算法设计方面,正确运用这两个关键词可以帮助编写更加灵活且高效的代码片段。 ```cpp // 示例展示 const auto 组合应用 void example() { const std::string strLiteral{"hello"}; // 推导出 const string& auto &&refToRvalueStr = strLiteral; // 尝试改变 refToRvalueStr 所绑定的内容会导致编译错误 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值