博客
关于我
c学习-37 关键字const学习
阅读量:509 次
发布时间:2019-03-07

本文共 3851 字,大约阅读时间需要 12 分钟。

1.用法

a. 修饰const修饰普通类型的变量b. const关键字修饰数组c. 常量指针与指针常量d. const参数传递和函数返回值。e. const修饰函数体f. const修饰类成员函数体

2. 注意点

a. const修饰的值,不是一定不可以改变的。

3. const与define的区别

a. 预编译指令只是对值进行简单的替换,不能进行类型检查b. 可以保护被修饰的东西,防止意外修改,增强程序的健壮性c. 编译器通常不为普通const常量分配存储空间,而是将它们保存在符号表中,这使得它成为一个编译期间的常量,没有了存储与读内存的操作,使得它的效率也很高

4.1 const修饰普通类型的变量 ,变量不能再修改值。

/* 1. 修饰const修饰普通类型的变量 */void test_23_01(){  const int a23_01 = 1;  int b23_01 = 1;  // a23_01=2;//报错“错误:向只读变量‘a23_01’赋值”  b23_01=2;  printf("a23_01=%d\n",a23_01);  printf("b23_01=%d\n",b23_01);}输出结果:    a23_01=1    b23_01=2

4.2  const修饰普通类型的变量 ,加上volatile关键字,变量能修改其值。

void test_23_02(){  const int a23_02 = 1;  volatile const int b23_02 = 1;  int *p1 = (int*)(&a23_02);//这这种强转并没有能修改a23_01值,输出结果a23_01还是为1  *p1 = 2;  int *p2 = (int*)(&b23_02);//定义的const变量,用volatile修饰,可以修改值,值变为了2  *p2 = 2;  printf("a23_02=%d\n",a23_02);  printf("b23_02=%d\n",b23_02);}输出结果:    a23_02=1    b23_02=2

4.3 const 修饰的数组,不能修改数组值。

/* const数组 */void test_23_03(){  const int a23[10]={1,2,3,4};  int b23[10]={1,2,3,4};  printf("a23[0]=%d\n",a23[0]);  printf("b23[0]=%d\n",b23[0]);  // a23[0]=5;//报错:“错误:向只读位置‘a23[0]’赋值  b23[0]=5;  printf("a23[0]=%d\n",a23[0]);  printf("b23[0]=%d\n",b23[0]);}输出结果:    a23[0]=1    b23[0]=1    a23[0]=1    b23[0]=5

4.4 常量指针与指针常量

/* 常量指针,const修饰的是(*p) */void test_23_04(){  int a1 = 4;  int b1 = 4;  const int *p1 = &a1;  int *p2 = &b1;  printf("*p1=%d\n",*p1);  printf("*p2=%d\n",*p2);  printf("a1=%d\n",a1);  printf("b1=%d\n",b1);  // *p1 = 5;//报错:“ 错误:向只读位置‘* p1’赋值”,常量指针,不能修改指针指向的值。  *p2 = 5;//非常量指针 ,可以修改指针指向的值。  printf("*p1=%d\n",*p1);  printf("*p2=%d\n",*p2);  printf("a1=%d\n",a1);  printf("b1=%d\n",b1);}输出结果:    *p1=4    *p2=4    a1=4    b1=4    *p1=4    *p2=5    a1=4    b1=5
/* 指针常量,const 修饰的是(p) */void test_23_05(){  int a1 = 4;  int b1 = 4;  int* const p1 = &a1;  int *p2 = &b1;  printf("*p1=%d\n",*p1);  printf("*p2=%d\n",*p2);  int a2 = 5;  int b2 = 5;  // p1 = &a2;//报错 : "错误:向只读变量‘p1’赋值"  p2 = &b2;  printf("*p1=%d\n",*p1);  printf("*p2=%d\n",*p2);输出结果:    *p1=4    *p2=4    *p1=4    *p2=5

4.5 const参数传递和函数返回值。

/* const参数传递,参数不能被修改 */void test_23_06_01(const int x){  // x++;// 报错: "错误:令只读形参‘x’自增"  printf("x=%d\n",x);}void test_23_06_02(int y){  y++;  printf("y=%d\n",y);}void test_23_06(){  int a1 = 4;  test_23_06_01(a1);  test_23_06_02(a1);}输出结果:    x=4    y=5

4.6 const修饰函数返回值

/* const修饰函数返回值-返回值为普通类型 */const int test_23_07_01(){  return 3;}int test_23_07_02(){  return 3;}void test_23_07(){  int a1 = test_23_07_01();//可以使用不用const修饰的变量接收  int b1 = test_23_07_02();  printf("a1=%d\n",a1);  printf("b1=%d\n",b1);  a1 = 5;//即便有const修饰,也可修改返回值。  b1 = 5;  printf("a1=%d\n",a1);  printf("b1=%d\n",b1);}输出结果:    a1=3    b1=3    a1=5    b1=5
/* const修饰函数返回值-返回值为指针 */const char* test_23_08_01(){  char *a = new char[10];  strcpy(a,"hello");  return a;}char* test_23_08_02(){  char *a = new char[10];  strcpy(a,"hello");  return a;}void test_23_08(){  // char *p1 = test_23_08_01();//报错:“错误:从类型‘const char*’到类型‘char*’的转换无效”  const char *p1 = test_23_08_01();//const修饰的指针可以接收  char *p2 = test_23_08_02();  printf("p1=%s\n",p1);  printf("p2=%s\n",p2);}输出结果:    p1=hello    p2=hello

4.7 const修饰函数体 (“一个函数名字后有const,这个函数必定是成员函数,

也就是说普通函数后面不能有const修饰....”)

/* const修饰函数体 */// void test_23_09_01() const {//   printf("this is test_23_09_01()\n");// }  /* 报错:“错误:非成员函数‘void test_23_09_01()’不能拥有 cv 限定符”(“一个函数名字后有const,这个函数必定是成员函数,也就是说普通函数后面不能有const修饰....”) */void test_23_09_02(){  printf("this is test_23_09_02()\n");}void test_23_09(){  // test_23_09_01();  test_23_09_02();}

4.8 const修饰类成员函数体

/* const修饰类成员函数体 */class A23_10{private:  int a1 = 1;public:  void fun1() const{    // a1++;//报错:"错误:increment of member ‘A23_10::a1’ in read-only object"    printf("a1=%d\n",a1);  }  void fun2() {    a1++;    printf("a1=%d\n",a1);  }};void test_23_10(){  A23_10 a23_10;  a23_10.fun1();  a23_10.fun2();}输出结果    a1=1    a1=2

参考: 

 

 

 

转载地址:http://lomjz.baihongyu.com/

你可能感兴趣的文章
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>