一 const 保护数组
#include <iostream>
using namespace std;
void modify(const int a[])
{
a++;
//a[0] += 10;
}
int main()
{
return 0;
}
二、const与指针
#include <iostream>]
using namespace std;
int main()
{
int k = 99;
const int *p;
p = &k;
//(*p)++ ;invalid
const int ci = 999;
p = &ci;//const value to const pointer
//int *pi = &ci; invalid
return 0;
}
这种情况在函数传参是尤其要注意。应当尽可能地使用const,因为这样既能保护数据,也能接受const和非const类型的数据。
另外
#include <iostream>]
using namespace std;
int main()
{
int age = 50;
const int *p = &age;
//(*p) += age; can not change the value of age;
int const *pp = &age;
int nage = age;
//pp = nage; can not change the value of the pointer
return 0;
}