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

nValue = 5;
int

*
const

pnPtr = &nValue;

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
= 6;
//
allowed, since pnPtr points to a non-const int

It is also possible to declare a pointer to a constant variable by using the const before the data type.

1
2
int

nValue = 5;
const

int

*pnPtr = &nValue;

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
= 6;
//
nValue is non-const

But the following is not:

1
*pnPtr
= 6;
//
pnPtr treats its value as const

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

nValue = 5;
int

nValue2 = 6;
 
const

int

*pnPtr = &nValue;
pnPtr
= &nValue2;
//
okay

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

int

nValue;
const

int

*
const

pnPtr = &nValue;

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,的更多相关文章

  1. 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 ...

  2. JavaScript Patterns 4.1 Functions Background

    Functions are first-class objects and they provide scope. • Can be created dynamically at runtime, d ...

  3. Nemerle Quick Guide

    This is a quick guide covering nearly all of Nemerle's features. It should be especially useful to a ...

  4. (原) c++ 杂

    Declaration of variables   C++ is a strongly-typed language, and requires every variable to be decla ...

  5. (转) Class

    Classes are an expanded concept of data structures: like data structures, they can contain data memb ...

  6. (转) Name visibility

    Scopes Named entities, such as variables, functions, and compound types need to be declared before b ...

  7. c++ namespace命名空间详解

    What is a namespace? A namespace defines an area of code in which all identifiers are guaranteed to ...

  8. CMake--Set用法

    CMake中的set用于给一般变量,缓存变量,环境变量赋值. cmake官方文档set set(<variable> <value> [[CACHE <type> ...

  9. compile time - run-time

    php.net Class member variables are called "properties". You may also see them referred to ...

随机推荐

  1. 四张类图理一下Streams的用法

    首先是输出流 OutputStream.继承它的类有两种,一种是底层实现(纯继承OutputStream的类),一种是格式转换(组合了OutputStream的类). 所谓的底层实现,就是真正和物理存 ...

  2. Android菜鸟的成长笔记(5)——Android系统源代码你下载了吗?

    原文:Android菜鸟的成长笔记(5)--Android系统源代码你下载了吗? 在上一篇中我们用Android系统源代码分析了我们前面写的代码,有的朋友可能就会问怎么才能下载到Google官方的源代 ...

  3. HDU3714 Error Curves (单峰函数)

    大意: 给你n个二次函数Si(x),F(x) = max{Si(x)} 求F(x)在[0,1000]上的最小值. S(x)=ax^2+bx+c       (0<=a<=100, |b|, ...

  4. Jenkins: 使用Jenkins搭建持续集成(CI)环境

    http://www.cnitblog.com/luckydmz/archive/2012/01/03/77007.html 首先从官网http://jenkins-ci.org/下载 Java We ...

  5. 从源代码角度分析ViewStub 疑问与原理

    一.提出疑问     ViewStub比較简单.之前文章都提及到<Android 性能优化 三 布局优化ViewStub标签的使用>.可是在使用过程中有一个疑惑,究竟是ViewStub上设 ...

  6. Spring MVC+JSP实现三级联动

    jsp代码 <script type="text/javascript"> $(function() { initProvinces(); }); /** * 获取省列 ...

  7. 【剑指offer】和为定值的两个数

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/24933341 题目描写叙述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的 ...

  8. thinkPHP 模板中的语法知识 详细介绍(十二)

    原文:thinkPHP 模板中的语法知识 详细介绍(十二) 本章节:介绍模板中的语法,详细的语法介绍 一.导入CSS和JS文件    ==>记住常量的是大写 1.css link .js  sc ...

  9. [置顶] C++为什么是C++而不是++C

    来自<C++ primer> 问:C++为什么是C++而不是++C 答 :C++之名是Rick Mascitti在1983年夏天定名,c说明它的本质实在C语言演化而来的,”++“是C语言的 ...

  10. 用VLC搭建流媒体server

    VLC开元项目相当强大,我们既能够将其作为播放核心用于二次开发,又能够将其作为高性能的流媒体server.今篇博客主要讲用VLC搭建流媒体server. VLC搭建流媒体server步骤非常easy: ...