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 ...
随机推荐
- Delphi XE7 用indy开发微信公众平台所有功能(10篇博客)
http://www.cnblogs.com/devinlee/p/4282498.html http://www.cnblogs.com/devinlee/p/4565933.html
- Delphi与Vista提供的UAC控制(1-代表资源编号,24-资源类型为RTMAINIFEST,最后用brcc32编译成资源文件)
Vista提供的UAC机制,是Vista的新增功能之一.它的主要目的是防止对于操作系统本身的恶意修 改.如果想对于Vista的 系统设置进行改动,必须通过UAC的验 证才能够进行.通过这样的手段,大大 ...
- 设置程序版本等信息(可直接修改pro文件设置,但是更推荐使用rc文件设置)
Qt版本:5.2.0 在.pro文件中设置版本等信息 VERSION = 1.2.3 QMAKE_TARGET_PRODUCT = 产品名称QMAKE_TARGET_COMPANY = 公司QMAKE ...
- 开源免费跨平台opengl opencv webgl gtk blender, opengl贴图程序
三维图形的这是opengl的强项,大型3D游戏都会把它作为首选.图像处理,是opencv的锁定的目标,大多都是C的api,也有少部分是C++的,工业图像表现,图像识别,都会考虑opencv的.webg ...
- Swift - 使用EventKit获取系统日历事件,添加事件
通过EventKit可以对iOS日历事件进行读取,添加等操作.但网上找到的都是使用Objective-C来编写的. 下面提供一个Swift版的样例,演示如何添加一个事件以及获取所有的事件列表. 1 2 ...
- Mysql免安装版脚本
使用Mysql过程中经常需要使用到免安装版本(绿色版)的Mysql,开始网上搜了一大堆,但还真是不怎么好用. 只好自己琢磨了一番,现在放出来和大家分享下: //安装启动服务 @ECHO OFF if ...
- Eclipse添加和查看书签
添加书签: 在编辑框左边栏右击 > Add Bookmark > 编辑书签名称(可以直接使用默认名称)> OK 查看书签: Window > Show View > Ot ...
- [Android学习笔记]some tips
集合合并去重: listA.removeAll(listB); listA.addAll(listB); android:singleLine="true"//单行显示 andro ...
- XMPP协议简介
XMPP(息处理现场协议)是基于可扩展标记语言(XML)的协议.它用于即时消息(IM)以及在线现场探測.XMPP协议採用的是client-server架构,全部从一个client发到还有一个clien ...
- leetcode解析回文子串拆分
转载请注明来自souldak,微博:@evagle Given a string s, partition s such that every substring of the partition i ...