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

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,

时间: 2024-10-12 00:49:33

Just like normal variables,的相关文章

what is the difference between static and normal variables in c++

void func() { static int static_var=1; int non_static_var=1; static_var++; non_static_var++; cout<<"Static="<<static_var; cout<<"NonStatic="<<non_static_var; } void main() { clrscr(); int i; for (i=0;i<5;i++)

Delphi DLL制作和加载 Static, Dynamic, Delayed 以及 Shared-Memory Manager

一 Dll的制作一般分为以下几步:1 在一个DLL工程里写一个过程或函数2 写一个Exports关键字,在其下写过程的名称.不用写参数和调用后缀.二 参数传递1 参数类型最好与window C++的参数类型一致.不要用DELPHI的数据类型.2 最好有返回值[即使是一个过程],来报出调用成功或失败,或状态.成功或失败的返回值最好为1[成功]或0[失败].一句话,与windows c++兼容.3 用stdcall声明后缀.4 最好大小写敏感.5 无须用far调用后缀,那只是为了与windows 1

C Reference Cheat Sheet by Ashlyn Black

原文网址:https://www.cheatography.com/ashlyn-black/cheat-sheets/c-reference/ Number Literals Integers 0b11111111 binary 0B11111111 binary 0377 octal 255 decimal 0xff hexadecimal 0xFF hexadecimal Real Numbers 88.0f / 88.1234567f single precision float ( f

Nemerle Quick Guide

This is a quick guide covering nearly all of Nemerle's features. It should be especially useful to anyone who is already familiar with C# or a similar language: Table of Contents Variables Operators Logical Operators Bit Operators Type Casts/Conversi

Cpp_with_MFC官方实例----定时器的使用

本文使用的实例是位于程序安装目录的prj文件夹下Cpp_with_MFC项目,该项目包括两个工程,分别是Cpp_with_MFC工程和kernel工程 kernel工程主要有两个函数组成: 1. _initFunction(void* pArgs) ,该函数是用于kernel工程生成的dll文件被外部调用时的初始化代码,该函数内部没有实质内容. 2. _timerCallBack(void* pArgs, void* pContext),该函数为定时器回调函数,每进入一次则计数值+1,每达到50

Native JavaScript Development after Internet Explorer

This article has nothing to do with the decision whether or not to abandon support for oldIE. You and you alone must take that decision based on the specific details of your website or application. With all this being said, let us proceed! 1. JavaScr

VJass

JassHelper 0.A.0.0 Although World Editor&apos;s Jass compiler was finally replaced by PJass using WEHelper , there were a couple of other annoyances that still needed fixing, that&apos;s the reason this project begand. Later I felt like going furt

References in C++

http://www.geeksforgeeks.org/references-in-c/ When a variable is declared as reference, it becomes an alternative name for an existing variable. A variable can be declared as reference by putting ‘&’ in the declaration. #include<iostream> using

OpenSCAD三维造型设计语言

cube Creates a cube at the origin of the coordinate system. When center is true the cube will be centered on the origin, otherwise it is created in the first octant. The argument names are optional if the arguments are given in the same order as spec