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++中,`const`指针结合使用有多种情况,其含义使用规则如下: ### const指针 `const`指针表示指针本身是一个常量,指针必须初始化,并且一旦初始化,其值(即存放在指针中的地址)就不能改变了,这个地址是一个对象的地址 [^1]。例如: ```cpp int num = 10; int* const ptr = # // ptr是const指针,指向num // ptr = &anotherNum; // 错误,不能改变ptr的指向 ``` ### 指向常量的指针 有两种写法:`const int* ptr` 或 `int const *ptr`,这种指针指向的值不能修改,但指针的指向可以修改 [^2]。根据C++语言规范,`const`修饰的是右侧离它最近的类型,在这种情况下,`const`修饰的是`int`,意味着不能通过该指针修改所指向的`int`类型的值,但指针本身可以指向其他内存地址 [^3]。示例代码如下: ```cpp int num1 = 10; int num2 = 20; const int* ptr = &num1; // *ptr = 30; // 错误,不能通过ptr修改指向的值 ptr = &num2; // 正确,可以改变ptr的指向 ``` ### 指针的指向不能修改,但指向的值可以修改 使用`int* const ptr`的形式,此时`const`修饰的是`ptr`,指针`ptr`现在是常量,不能指向其他内存,但可通过指针解引用修改指向内存的值 [^2][^3]。示例如下: ```cpp int num = 10; int* const ptr = # *ptr = 20; // 正确,可以修改指向的值 // ptr = &anotherNum; // 错误,不能改变ptr的指向 ``` ### 既不能修改指针的指向,也不能修改指向的值 使用`const int *const ptr`的形式,左边`const`修饰`int *`,右边`const`修饰`ptr`,表明指向内存解引用修改值都不行 [^2][^3]。示例代码如下: ```cpp int num = 10; const int *const ptr = # // *ptr = 20; // 错误,不能修改指向的值 // ptr = &anotherNum; // 错误,不能改变ptr的指向 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值