Just like normal variables,
Just like normal variables, pointers can be declared constant. There are two different ways that pointers and const can be intermixed, and they are very easy to mix up.
To declare a const pointer, use the const keyword between the asterisk and the pointer name:
1
2
|
int
int
const
|
Just like a normal const variable, a const pointer must be initialized to a value upon declaration, and its value can not be changed. This means a const pointer will always point to the same value. In the above case, pnPtr will always point to the address of
nValue. However, because the value being pointed to is still non-const, it is possible to change the value being pointed to via dereferencing the pointer:
1
|
*pnPtr // |
It is also possible to declare a pointer to a constant variable by using the const before the data type.
1
2
|
int
const
|
Note that the pointer to a constant variable does not actually have to point to a constant variable! Instead, think of it this way: a pointer to a constant variable treats the variable as constant when it is accessed through the pointer.
Thus, the following is okay:
1
|
nValue // |
But the following is not:
1
|
*pnPtr // |
Because a pointer to a const value is a non-const pointer, the pointer can be redirected to point at other values:
1
2
3
4
5
|
int
int
const
pnPtr // |
Confused?
To summarize:
- A non-const pointer can be redirected to point to other addresses.
- A const pointer always points to the same address, and this address can not be changed.
- A pointer to a non-const value can change the value it is pointing to.
- A pointer to a const value treats the value as const (even if it is not), and thus can not change the value it is pointing to.
Finally, it is possible to declare a const pointer to a const value:
1
2
|
const
const
const
|
A const pointer to a const value can not be redirected to point to another address, nor can the value it is pointing to be changed.
Const pointers are primarily used for passing variables to functions. We will discuss this further in the section on functions.
版权声明:本文博主原创文章,博客,未经同意不得转载。
Just like normal variables,的更多相关文章
- C# 7 out variables, tuples & other new features
C# 7 out variables, tuples & other new features C# 7 is available on new Visual Studio 2017 and ...
- JavaScript Patterns 4.1 Functions Background
Functions are first-class objects and they provide scope. • Can be created dynamically at runtime, d ...
- Nemerle Quick Guide
This is a quick guide covering nearly all of Nemerle's features. It should be especially useful to a ...
- (原) c++ 杂
Declaration of variables C++ is a strongly-typed language, and requires every variable to be decla ...
- (转) Class
Classes are an expanded concept of data structures: like data structures, they can contain data memb ...
- (转) Name visibility
Scopes Named entities, such as variables, functions, and compound types need to be declared before b ...
- c++ namespace命名空间详解
What is a namespace? A namespace defines an area of code in which all identifiers are guaranteed to ...
- CMake--Set用法
CMake中的set用于给一般变量,缓存变量,环境变量赋值. cmake官方文档set set(<variable> <value> [[CACHE <type> ...
- compile time - run-time
php.net Class member variables are called "properties". You may also see them referred to ...
随机推荐
- Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上)
原文:[置顶] Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上) 我们在用手机的时候可能会发现,即使应用被放到后台再返回到前台数据依然保留(比如说我们正在玩游戏,突然电话 ...
- RPC 的概念模型与实现解析(转)
今天分布式应用.云计算.微服务大行其道,作为其技术基石之一的 RPC 你了解多少?一篇 RPC 的技术总结文章,数了下 5k+ 字,略长,可能也不适合休闲的碎片化时间阅读,可以先收藏抽空再细读:) 全 ...
- 打开excel2007文档时显示“excel无法打开文件~$book.xltm”
此问题的出现是因为意外情况导致Excel临时文件没有删掉,把C:\Program Files\Microsoft Office\Office12\XLSTART里面的临时文件“~$book”给删除就好 ...
- c++ virturn function -- 虚函数
c++ virturn function -- 虚函数 pure irtual function -- 纯虚函数 先看例子 #include <iostream> using nam ...
- HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
- [eclipse] 三个操作技巧
[eclipse] 三个操作技巧 1.快捷键Ctrl+Shift+i:Debug调试中直接获取方法的返回值 在下图代码中,想知道getHost(),则在调试时运行完该句代码后,选中"urlU ...
- MongoDB -- 更新
$pull: db.collection.update( <query>, { $pull: { <arrayField>: <query2> } } ) $pul ...
- hadoop 磁盘限额配置
配置方法: 在 hdfs-site.xml 里配置如下参数,注意,那个 value 的值是配置该磁盘保留的DFS不能使用的空间大小,单位是字节. (如果多块硬盘,则表示为每块硬盘保留这么多空间) &l ...
- Mysql免安装版脚本
使用Mysql过程中经常需要使用到免安装版本(绿色版)的Mysql,开始网上搜了一大堆,但还真是不怎么好用. 只好自己琢磨了一番,现在放出来和大家分享下: //安装启动服务 @ECHO OFF if ...
- Java 5种字符串拼接方式性能比较。
最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试. 代码如下: import java.util.ArrayList; import java.uti ...