- 如果对象不是针对,它们没有区别
int const x = 3;
const int x = 3;
- 如果对象是指针,它们有区别
int* const p = &array: //指针p不能够指向其他地址
const int* p = &arr //指针p只读&array,不能够对其进行修改
#include <iostream>
using namespace std;
int main()
{
int arr[3] = {1, 2, 3};
int varr[3] = {100, 200, 300};
const int *p1 = arr; //常量指针,指针值不能修改,记忆方法,const旁边是一个int是一个值
int *const p2 = arr; //指针常量,指针指向地址不能修改 const旁边是一个*是一个地址
cout << *p1 << endl;
cout << *p2 << endl;
// *p1 = 22; // error
// *p2 = 22;
// cout << *p2 << endl;
// cout << arr[0] << endl;
p1 = varr;
cout << *p1 << endl;
p2 = varr; //error
return 0;
}