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
|
intintconst |
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
|
intconst |
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
|
intintconst 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 ...
随机推荐
- 所有CM_消息的说明
这些CM消息,居然在Delphi的帮助里是没有任何说明的,真是昏倒.意外在高手的书里找到了所有说明,说明如下: Message Constant Value Description cm_Base $ ...
- Swift - 解析XML格式数据(分别使用GDataXML和DDXML)
在做一些应用的时候经常需要用到XML解析,比如获取Web Service数据,读取RSS新闻或者博客数据源.下面演示了两个非常方便高效的XML库在Swift里的调用方法. 假设需要被解析的XML数据文 ...
- 2014年Windows平台软件推荐:神器小工具(骨灰级
原文 http://www.wtoutiao.com/a/120621.html 底层工具 “If you know how to use Process Monitor competently, ...
- ios block循环引用问题
ios开发中,开了ARC模式,系统自动管理内存,如果程序中用到了block就要注意循环引用带来的内存泄露问题了 这几天遇到一个问题,正常页面dismiss的时候是要调用dealloc方法的,但是我的程 ...
- NSDate的处理:前一天、后一天等关于时区偏移的处理以及在数据库中的使用
看来非常多网上关于日期的处理,今天.昨天.前天.后天.一周等,满心欢喜的拿着去验证结果总是不尽如人意,看别别人的代码看的脑涨.自己就写一个简单的,自己来用,以后用其它的方法,我会在完好,再次记录.以方 ...
- ubuntu12 环境下编译freerdp
有时候需要从linux环境下远程连接到windows的环境,可以采用freerdp.freerdp是一个linux下开源的工具,在Ubuntu下可以直接用 apt-get install freerd ...
- ps中图层混合模式算法公式
网上已经有很多讲解ps的图层混合模式,有些不详细甚至是错误的,参考网上给出的公式及其自己在验证推倒的,给出27种的混合模式算法公式.也许存在一定的错误性,毕竟没有官方给出公式,只能说以供参考吧. 只考 ...
- java学习笔记08--泛型
java学习笔记08--泛型 泛型可以解决数据类型的安全性问题,它主要的原理,是在类声明的时候通过一个标识标识类中某个属性的类型或者是某个方法的返回值及参数类型.这样在类声明或实例化的时候只要指定好需 ...
- 用C语言写解释器(一)——我们的目标
声明 为提高教学质量,我所在的学院正在筹划编写C语言教材.<用C语言写解释器>系列文章经整理后将收入书中"综合实验"一章.因此该系列的文章主要阅读对象定为刚学完C语言的 ...
- Xah Lee Web 李杀网
Xah Lee Web 李杀网 ∑ Xah Lee Web 李杀网