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 ...
随机推荐
- ExtJS拖拽效果
ExtJS拖拽效果 <html> <head> <title>hello</title> <meta http-equiv="conte ...
- 积累的VC编程小技巧之编辑框
1.如何让对话框中的编辑框接收对话框的消息 ////////////////////////////////////////////////// 如何让对话框中的CEdit控件类接收对话框的消息/// ...
- MFC消息响应机制分析
---- 摘要: ---- MFC是Windows下程序设计的最流行的一个类库,但是该类库比较庞杂,尤其是它的消息映射机制,更是涉及到很多低层的东西,我们在这里,对它的整个消息映射机制进行了系统的分析 ...
- QT插件开发方式(没看懂)
创建一个QT的库项目,删除自动生成的.h和.cpp文件,添加一个接口定义.h文件和一个接口实现类(一个.h一个.cpp).代码如下: 1.接口文件源码 #ifndef PLUGININTERFACE_ ...
- 推动Common Lisp的实际应用
推动Common Lisp的实际应用 推动Common Lisp的实际应用
- 深入 理解 Statement 和 PreparedStatement
一.使用Statement而不是PreparedStatement对象 JDBC驱动的最佳化是基于使用的是什么功能. 选择PreparedStatement还是Statement取决于你要怎么使用它们 ...
- Java程序员们最常犯的10个错误(转)
1.将数组转化为列表 将数组转化为一个列表时,程序员们经常这样做: 1 List<String> list = Arrays.asList(arr); Arrays.asList(&quo ...
- servlet其工作原理和例子证明
servlet简单介绍 当我们在地址栏里面输入www.baidu.com,终于呈如今我们面前的是百度搜索的页面.在这些訪问过程中,都会有一个webserver来处理这些请求以及訪问处理后的结果. 而s ...
- ArcGIS 10.2 操作SQLite
SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它.ArcGIS 10.2 提供了对SQLite数据库的支持,这对那 ...
- poj3662(二分+最短路)
题目连接:http://poj.org/problem?id=3662 题意:有n个节点p条无向边,现在可以选择其中的任意K条免费,则花费为除了k条边后权值最大的一个,求最小花费多少. 分析:二分枚举 ...