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. 利用SQL语句实现分页

    1.概述 在网页中如果显示的数据太多就会占据过多的页面,而且显示速度也会很慢.为了控制每次在页面上显示数据的数量,就可以利用分页来显示数据. 2.技术要点 在SQL Server中要实现SQL分页,需 ...

  2. 步步为营Hibernate全攻略(四)剪不断理还乱之:复合主键 && 组合映射

    一:复合主键 复合主键即两个或多个字段联合起来作为主键,它的通常做法是将主键相关字段抽取出来放到一个单独的类中,但是这样的类是有要求的: 1.      必须实现序列化接口 2.      必须覆盖e ...

  3. php获取server端mac和clientmac的地址

    获取servermac <?php /** 获取网卡的MAC地址原码:眼下支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetmacAddr{ var $re ...

  4. java中Executor、ExecutorService、ThreadPoolExecutor介绍(转)

    1.Excutor 源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /**     * Executes th ...

  5. NYOJ 914 Yougth的最大化

    Yougth的最大化 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 Yougth如今有n个物品的重量和价值各自是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价 ...

  6. poj 1094

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26911   Accepted: 92 ...

  7. 基于HOG特征的Adaboost行人检测

    原地址:http://blog.csdn.net/van_ruin/article/details/9166591 .方向梯度直方图(Histogramof Oriented Gradient, HO ...

  8. Spark SQL Catalyst源代码分析Optimizer

    /** Spark SQL源代码分析系列*/ 前几篇文章介绍了Spark SQL的Catalyst的核心运行流程.SqlParser,和Analyzer 以及核心类库TreeNode,本文将具体解说S ...

  9. 【ThinkingInC++】52、函数内部的静态变量

    /** * 书本:[ThinkingInC++] * 功能:函数内部的静态变量 * 时间:2014年9月17日18:06:33 * 作者:cutter_point */ #include " ...

  10. Invalid embedded descriptor for ".proto".Dependencies passed (Protobufer)解决办法

    前言 之前开发的时候,发现居然出现了Dependencies passed to FileDescriptor.buildFrom() don't match those listed in the ...