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. Lucene.Net 2.3.1开发介绍 —— 四、搜索(二)

    原文:Lucene.Net 2.3.1开发介绍 -- 四.搜索(二) 4.3 表达式用户搜索,只会输入一个或几个词,也可能是一句话.输入的语句是如何变成搜索条件的上一篇已经略有提及. 4.3.1 观察 ...

  2. 排序(6)---------归并排序(C语言实现)

    归并排序: 归并操作,也叫归并算法,指的是将两个已经排序的序列合并成一个序列的操作.归并排序算法依赖归并操作. 归并操作的步骤例如以下:     (1) 申请空间,使其大小为两个已经排序序列之和.该空 ...

  3. Successful Lisp - Cover

    Successful Lisp - Cover Successful Lisp: How to Understand and Use Common Lisp

  4. SAE php 研究(2)

    1.在SAE新建项目打印出phpinfo <?php  print phpinfo(); ?> 2. 可见:PHP Version 5.3.8 [使用的是php5.3.8编译的] 3. 可 ...

  5. cannot load supported formats intellij 解决的方法

    http://stackoverflow.com/questions/20797443/intellij-idea-subversion-checkout-error http://stackover ...

  6. ESP8266学习笔记1:怎样在安信可全功能測试板上实现ESP-01的编译下载和调试

    近期调试用到了安信可的ESP-01模块,最终打通了编译下载调试的整个通道,有一些细节须要记录,方便兴许的开发工作. 转载请注明:http://blog.csdn.net/sadshen/article ...

  7. jQuery遍历函数

    jQuery遍历函数包含了用于筛选.查找和串联元素的方法. .add():将元素加入到匹配元素的集合中. .andSelf():把堆栈中之前的元素集加入到当前集合中. .children():获得匹配 ...

  8. 使用Jquery+EasyUI项目开发情况的框架是中评---员工管理源代码共享

    使用Jquery+EasyUI 进行框架项目开发案例解说之中的一个 员工管理源代码分享 在開始解说之前,我们先来看一下什么是Jquery EasyUI?jQuery EasyUI是一组基于jQuery ...

  9. RIO包 健壮的I/O函数代码

    下面是关于 #include <stdio.h> #include <string.h> #include <errno.h> #include <sys/t ...

  10. ssh登录过程详细介绍

    服务器端和客户端就取得了相同的会话密钥和会话 ID .对于后续传输的数据,两端都会使用会话密钥进行加密和解密,保证了数据传送的安全. http://blog.csdn.net/lhq9220/arti ...