const用法大全:
https://www.cnblogs.com/lanjianhappy/p/7298427.html
修改const int i;
https://blog.csdn.net/weixin_41413441/article/details/80860135
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a=100;//a为变量
int *const cp = &a; //cp为 常量指针int型。即cp指针不能改变,但cp指针指向的内容可以改变。
*cp = 200;
cout << "a" << a<<endl;//输出200
int const* p = &a;//p为指针常量int型。即p为指针可以改变,但p指针指向的内容不可以通过p指针来改变。
int *pp = (int *)p;//pp指针赋值p,可以通过pp指针来修改p指针指向的内容。
*pp = 300;
cout << "a" << a<<endl;//输出300
int b = 10;
p = &b;
cout << "*p" << *p << endl;
int c = 20;
const int& rc = c; //rc引用c,不能通过rc该引用来修改c的值。
cout <<"rc"<< rc << endl;//输出20
int& const rcc = c;//与上面一样
cout << "rcc" << rcc << endl;//输出20
const int d = 11; //d为常量11
int *dp = (int*)&d;//dp指针指向d的地址
*dp = 12;//通过指针来修改常量d
cout << *dp << endl;//输出12
cout<< d << endl;//输出11
cout << dp << endl;//
cout << &d << endl;//两地址相同,原因为const int d 常量的值放在了符号表中,而指针*dp修改的内存的值。
const volatile int dd = 11; //加上volatile修改符,即告诉编译器该变量属于易变的,不要对此句进行优化,每次计算时要去内存中取数
int *ddp = (int*)ⅆ
*ddp = 12;
cout << *ddp << endl;//输出12,从内存取数
cout << dd << endl;//输出12,从内存取数
cout << ddp << endl;//输出地址
cout << &(*ddp) << endl;
}