指针的const应用
分为三个
一、const修饰指针
我允许你更换存储的门牌号,但是这个里面住户的数据布局不许改
#include<iostream>
using namespace std;int main()
{int a = 10, b=20,c=10; const int* p = &a; //const修饰指针*p = 10; //不允许const对于p指针指向的"值"修改p = &b; //允许const对于p指针指向的修改system("pause");return 0;
}
二、const修饰常量
我不允许你更换存储的门牌号,这个里面住户的数据布局随便你改
#include<iostream>
using namespace std;int main()
{int a = 10, b=20,c=10; int* const p = &a; //const修饰常量*p = 100; //允许const对于p指针指向的"值"修改p = &b; //不允许const对于p指针"指向"的修改system("pause");return 0;
}
三、const修饰指针和常量
我不允许你更换存储的门牌号,这个里面住户的数据布局也不许改
#include<iostream>
using namespace std;int main()
{int a = 10, b=20,c=10; const int* const p = &a; //const修饰指针和常量*p = 10; //不允许const对于p指针指向的"值"修改p = &b; //不允许const对于p指针"指向"的修改system("pause");return 0;
}