const

const只读变量,具有不可变性

 
注意:const修饰的只读变量必须在定义的同时初始化
 
(case语句后面可以是修饰的只读变量,或是整型或字符型的常量或常量表达式)
 
可修饰
1、一般变量
int const i=2;或const int i=2;
2、修饰数组
int const array[5]={1,2,3,4,5};或
const int array[5]={1,2,3,4,5}
3、修饰指针(小技巧:看const离哪个进)
const int *p;或int const *p;//p(即地址)可变,p指向的对象不可变
int *const p;      //p(即地址)不可变,p指向的对象可变(即内容)
4、修饰函数的参数
当不希望函数值被函数体内意外改变时,用const修饰符
void fun(const int i);
5、修饰函数的返回值
即返回值不可被改变
const int fun(int i);

 

原文地址:https://www.cnblogs.com/Alling/p/3971822.html