Just like normal variables,
来源:互联网 发布:php自动加载机制 编辑:程序博客网 时间:2024/04/30 10:33
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:
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:
*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.
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:
nValue = 6;
// nValue is non-const
But the following is not:
*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:
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:
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,
- Just like normal variables,
- just like every time
- 【英语】something just like this
- about CString of EVC(just like Win98)
- 数据量大的操作注意:SHOW VARIABLES like 'max_allowed_packet'
- 经过show variables like xxx 详解mysql运行时参数
- Mysql 查看连接数,状态 show variables like '%max_connections%'
- normal
- Normal
- I was just like a frog in the well
- The first blog on CSDN, just like sofa~~
- openoffice connect mysql jdbc/odbc(just like access to mssql)
- [ML of Andrew Ng]Week 2 : Linear Regression with Multiple Variables and Normal Equation
- Variables
- Variables
- Variables
- variables
- In order to understandC++
- DB2数据库基础总结
- mysql 开启远程登录
- UML静态模型图
- XStream 用法详解 XML 转换为 java 实体类
- Just like normal variables,
- dataGridView 新增自动排序
- In all of the functions
- Java String Buffer
- python移植到arm
- VMware虚拟机优化10种技巧
- static member variables
- 给定0到4的随机数的方法,产生0到6的随机数的方法
- 大突破!人类有望饿死癌细胞