文档章节

More About const

RenrenC
 RenrenC
发布于 2016/07/06 14:32
字数 654
阅读 4
收藏 0

Earlier, you saw that you can use const to create symbolic constants:
 const double PI = 3.14159;
That was something you could do with the #define directive, too, but const additionally lets
you create constant arrays, constant pointers, and pointers to constants.
Listing 10.4showed how to use the const keyword to protect an array:
 #define MONTHS 12
 ...
 const int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};
If the program code subsequently tries to alter the array, you’ll get a compile-time error
message:
 days[9] = 44; /* compile error */
Pointers to constants can’t be used to change values. Consider the following code:
 double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5};
 const double * pd = rates; // pd points to beginning of the array
The second line of code declares that the type double value to which pd points is a const .
That means you can’t use pd to change pointed-to values:
 *pd = 29.89; // not allowed
 pd[2] = 222.22; // not allowed
 rates[0] = 99.99; // allowed because rates is not const
Whether you use pointer notation or array notation, you are not allowed to use pd to change
the value of pointed-to data. Note, however, that because rates was not declared as a constant, 

you can still use rates to change values. Also, note that you can make pd point somewhere
else:
 pd++; /* make pd point to rates[1] -- allowed */
A pointer-to-constant is normally used as a function parameter to indicate that the function
won’t use the pointer to change data. For example, the show_array() function from Listing
 10.14could have been prototyped as
 void show_array(const double *ar, int n);
There are some rules you should know about pointer assignments and const. First, it’s valid to
assign the address of either constant data or non-constant data to a pointer-to-constant:
 double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5};
 const double locked[4] = {0.08, 0.075, 0.0725, 0.07};
 const double * pc = rates; // valid
 pc = locked; // valid
 pc = &rates[3]; // valid
However, only the addresses of non-constant data can be assigned to regular pointers:
 double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5};
 const double locked[4] = {0.08, 0.075, 0.0725, 0.07};
 double * pnc = rates; // valid
 pnc = locked; // not valid
 pnc = &rates[3]; // valid
This is a reasonable rule. Otherwise, you could use the pointer to change data that was
supposed to be constant.
A practical consequence of these rules is that a function such as show_array() can accept
the names of regular arrays and of constant arrays as actual arguments, because either can be
assigned to a pointer-to-constant:
 show_array(rates, 5); // valid
 show_array(locked, 4); // valid
Therefore, using const in a function parameter definition not only protects data, it also allows
the function to work with arrays that have been declared const .
A function such as mult_array(), however, shouldn’t be passed the name of a constant array
as an argument:
 mult_array(rates, 5, 1.2); // valid
 mult_array(locked, 4, 1.2); // bad idea 

What the C standard says is that an attempt to modify const data, such as locked, using a non- const identifier, such as the mult_array() formal argument ar, results in undefined behavior.

There are more possible uses of const. For example, you can declare and initialize a pointer so that it can’t be made to point elsewhere. The trick is the placement of the keyword const : double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5}; double * const pc = rates; // pc points to beginning of the array pc = &rates[2]; // not allowed to point elsewhere *pc = 92.99; // ok -- changes rates[0] Such a pointer can still be used to change values, but it can point only to the location originally assigned to it. Finally, you can use const twice to create a pointer that can neither change where it’s pointing nor change the value to which it points: double rates[5] = {88.99, 100.12, 59.45, 183.11, 340.5}; const double * const pc = rates; pc = &rates[2]; // not allowed *pc = 92.99; // not allowed

© 著作权归作者所有

共有 人打赏支持
上一篇: array VS pointer
下一篇: C
RenrenC
粉丝 0
博文 7
码字总数 1086
作品 0
苏州
私信 提问
一个Autoit恶意资源消耗样本

文件md5:b1c05acd8cbbf1bd9a2448da0c10c411 中毒迹象 基本信息获取 基本PE信息 解码后脚本为: //2017-12-1 22:43 NoTrayIcon RequireAdmin Region AutoIt3Wrapper_icon=C:Windowssystem32......

Alyoyojie
2017/12/01
0
0
C++ const 详解

Const 是C++中常用的类型修饰符,常类型是指使用类型修饰符const说明的类型,常类型的变量或对象的值是不能被更新的。const只在编译期保证所修饰对象的不可修改。 一、Const作用 如下表所示:...

sharep
2013/07/23
0
0
“指向const对象的指针”和“const指针”

1、指向const对象的指针 指向const对象的指针就是一个指针,不能通过它来修改它所指向的对象的值 · 声明方法:const int p; const对象在初始化后是不允许对其值进行修改的,因此,我们不能用...

徐燕
2011/07/27
0
0
C++ 惯用法: const 常量和字面量

原文:C++ 惯用法: const 常量和字面量 作者:Breaker C++ 中 const 关键字、常量和字面量的惯用法 关键字:const, enum, const_cast, const_iterator, mutable, 左值, 字面量 本质和非本质上...

晨曦之光
2012/05/23
495
0
C++难点解析之const修饰符

C++难点解析之const修饰符 c++ 相比于其他编程语言,可能是最为难掌握,概念最为复杂的。结合自己平时的C++使用经验,这里将会列举出一些常见的难点并给出相应的解释。 const修饰符 const在c...

jackie8tao
2018/06/22
0
0

没有更多内容

加载失败,请刷新页面

加载更多

C++ vector和list的区别

1.vector数据结构 vector和数组类似,拥有一段连续的内存空间,并且起始地址不变。 因此能高效的进行随机存取,时间复杂度为o(1); 但因为内存空间是连续的,所以在进行插入和删除操作时,会造...

shzwork
今天
3
0
Spring之invokeBeanFactoryPostProcessors详解

Spring的refresh的invokeBeanFactoryPostProcessors,就是调用所有注册的、原始的BeanFactoryPostProcessor。 相关源码 public static void invokeBeanFactoryPostProcessors(Configu......

cregu
昨天
4
0
ibmcom/db2express-c_docker官方使用文档

(DEPRECIATED) Please check DB2 Developer-C Edition for the replacement. What is IBM DB2 Express-C ? ``IBM DB2 Express-C``` is the no-charge community edition of DB2 server, a si......

BG2KNT
昨天
3
0
Ubuntu 18.04.2 LTS nvidia-docker2 : 依赖: docker-ce (= 5:18.09.0~3-0~ubuntu-bionic)

平台:Ubuntu 18.04.2 LTS nvidia-docker2 版本:2.0.3 错误描述:在安装nvidia-docker2的时候报dpkg依赖错误 nvidia-docker2 : 依赖: docker-ce (= 5:18.09.0~3-0~ubuntu-bionic) 先看一下依......

Pulsar-V
昨天
4
0
学习笔记1-goland结构体(struct)

写在前面:若有侵权,请发邮件by.su@qq.com告知。 转载者告知:如果本文被转载,但凡涉及到侵权相关事宜,转载者需负责。请知悉! 本文永久更新地址:https://my.oschina.net/bysu/blog/3036...

不最醉不龟归
昨天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部