指针和Const

指针中Const的用法分为指向常量的指针常量指针。

一、指向常量的指针(Pointers to constants)

· 不能改变所指向的值

/*
在定义指针时必须使用const关键字
*/
const double interest_rate[]={0.3,0.4,0.7};
const double *ratePtr;

例如:const int *size

其中,"*"表示size指向的是一个指针,"const int"说明size指向的是一个int类型的常量

二、常量指针(Constant pointer)

· 一旦指针被初始化,指针地址就不能更改(即指针本身是常量)

/*
用与变量名相连的const关键字定义;
且在定义时就必须初始化;
*/
int size=24;
int * const ptr = &size;

· 指针中的地址不能改变,但是该地址上的数据可以改变。

例如:int * const ptr

其中,"* const"表示ptr是一个常量指针,ptr指向的是int类型

三、指向常量的常量指针(Constant pointers to constants)

/*
一个指针只能指向它所指向的东西
*/
int value = 222;
const int * const ptr = &value;

例如:const int * const ptr

其中,"* const"表示ptr是一个常量指针,ptr指向的是"const ptr"

我们来看看几个例子作对比:

例一、

/*
Regular pointer
*/
#include<iostream>
using namespace std;
int main(){
    int value1 = 123;
    int value2 = 456;
    int *ptr = &value1;
    cout<<"Regular pointer\n";
    cout<<"value1: "<<value1<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
    *ptr = 123456;
    cout<<"value1: "<<value1<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
    ptr = &value2;
    cout<<"value2: "<<value2<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
}

输出:
Regular pointer
value1: 123 ptr: 0x75d8ab09b318 *ptr: 123
value1: 123456 ptr: 0x75d8ab09b318 *ptr: 123456
value2: 456 ptr: 0x75d8ab09b31c *ptr: 456

· 既可以改变指针的值,也能改变指针所指向的地址

例二、

/*
Constant pointer
*/
#include<iostream>
using namespace std;
int main(){
    int value3 = 11;
    int value4 = 22;
    int * const ptr = &value3;
    cout<<"Constant pointer\n";
    cout<<"value3: "<<value3<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
    value3 = 33;
    cout<<"value3: "<<value3<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
    *ptr = 123;
    cout<<"value3: "<<value3<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
}
// ptr = &value4; 指针ptr在定义时地址已经被初始化,所以指针指向的地址不能被改变

输出:
Constant pointer
value3: 11 ptr: 0x74bc8f20cd2c *ptr: 11
value3: 33 ptr: 0x74bc8f20cd2c *ptr: 33
value3: 123 ptr: 0x74bc8f20cd2c *ptr: 123

· (常量指针)不能改变指针所指向的地址,只能改变值

例三、

/*
Pointer to a constant
*/
#include<iostream>
using namespace std;
int main(){
    int value5 = 555;
    int value6 = 666;
    const int * ptr = &value5;
    cout<<"Pointer to a constant\n";
    cout<<"value5: "<<value5<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
    value5 = 777;
    cout<<"value5: "<<value5<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
    ptr = &value6;
    cout<<"value6: "<<value6<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
}
// *ptr = 123; 不能改变值

输出:
Pointer to a constant
value5: 555 ptr: 0x7e3da4155458 *ptr: 555
value5: 777 ptr: 0x7e3da4155458 *ptr: 777
value6: 666 ptr: 0x7e3da415545c *ptr: 666

· (指向常量的指针)可以改变地址,但是不能改变值

例四、

/*
Constant pointer to a constant
*/
#include<iostream>
using namespace std;
int main(){
    int value7 = 8989;
    int value8 = 1313;
    const int * const ptr = &value7;
    cout<<"Constant pointer to a constant\n";
    cout<<"value7: "<<value7<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
    value7 = 777;
    cout<<"value7: "<<value7<<" ptr: "<<ptr<<" *ptr: "<<*ptr<<endl;
   }
// ptr7 = &value8;
// *ptr = 111;  指针和值都不能改变

输出:
Constant pointer to a constant
value7: 8989 ptr: 0x7bace3262c8c *ptr: 8989
value7: 777 ptr: 0x7bace3262c8c *ptr: 777

· (指向常量的常量指针)地址和值都不能改变

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值