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 ...
随机推荐
- 推荐一个第三方Qt库的集合
https://inqlude.org/ Stable libraries | Development versions | Unreleased | Commercial | All attica ...
- Android菜鸟的成长笔记(7)——什么是Activity
原文:[置顶] Android菜鸟的成长笔记(7)——什么是Activity 前面我们做了一个小例子,在分析代码的时候我们提到了Activity,那么什么是Activity呢? Activity是An ...
- CAS (1) —— Mac下配置CAS到Tomcat(服务端)(转)
tomcat版本: tomcat-8.0.29 jdk版本: jdk1.8.0_65 cas版本: cas4.1.2cas-client-3.4.1 参考来源: CAS实现单点登录(SSO)经典完整教 ...
- 一句话解释JVM中空间分配担保的问题
先解释YGC: 当对象生成在EDEN区失败时,出发一次YGC,先扫描EDEN区中的存活对象,进入S0区,S0放不下的进入OLD区,再扫描S1区,若存活次数超过阀值则进入OLD区,其它进入S0区,然后S ...
- Nancy.Host的Web应用
Nancy.Host实现脱离iis的Web应用 本篇将介绍如何使用Nancy.Host实现脱离iis的Web应用,在开源任务管理平台TaskManagerV2.0代码里面已经使用了Nancy.Host ...
- 更好的自动ssh登录
更好的自动ssh登录 解决~/.ssh/known_hosts 过期问题. bash + expect bash:ssh.sh #!/bin/bash help(){ echo "usage ...
- 魔棒工具--RegionGrow算法简介
原地址:http://www.cnblogs.com/easymind223/archive/2012/07/04/2576964.html ps里面的魔棒工具非常好用,是图像处理中非常常用的一个工具 ...
- haproxy 中的http请求和https请求
use Mojolicious::Lite; use JSON qw/encode_json decode_json/; use Encode; no strict; use JSON; # /foo ...
- ssh-copy-id -i ~/.ssh/id_rsa.pub admin@172.17.42.66
ssh-copy-id -i ~/.ssh/id_rsa.pub admin@172.17.42.66
- LAN路由
一.实验的目的: 实现不同子网之前的信息交流 二.如果 1.虚拟子网 VMnet8:192.168.233.0/24 VMnet1:172.16.1.0/24 2.虚拟机vm1 ip:1 ...