Constant Pointer vs Pointer to constant

from : Constant Pointer vs Pointer to constant
https://www.linkedin.com/pulse/constant-pointer-vs-shirley-vijayakumar

 

Reading a declaration is confusing most of the time, but there are few tricks:

1. Always read it from the right hand side, it is also called as Spiral Rule. 

2. When you see *, read it as 'pointer to'

int* - pointer to int
int const * - pointer to const int
int * const - const pointer to int
int const * const - const pointer to const int

The constant before * can be on either side of the type, but both refer to "pointer to constant int" or "constant pointer to constant int"

const int * == int const *
const int * const == int const * const

For the double pointer, the declaration can be read as:

int ** - pointer to pointer to int
int ** const - a const pointer to a pointer to an int
int * const * - a pointer to a const pointer to an int
int const ** - a pointer to a pointer to a const int
int * const * const - a const pointer to a const pointer to an int

To read the * declaration, we can use Spiral Rule. Click here to read. 

Constant Pointer (int * const)

  • Constant pointer defines that the pointer is constant but not its value. This indicates that the value can be changed.
  • Constant pointer can't be declared without initialisation. 
  • If the const keyword appears right side of *, the pointer is constant. 
#include <iostream>
using namespace std;
int main ()
{
int c = 15;
int *const bar; // constant pointer to int
cout << "Address of bar: " << bar << " Value: " << *bar << endl;
*bar = 16; // the value of c can be changed through the pointer.
cout << "bar holds value 16 now!! " << endl
<< "Address of bar: " << bar << " Value: " << *bar << endl << endl;
return 0;
}

 

Output:1
2
3
In function 'int main()':
Line 9: error: uninitialized const 'bar'
compilation terminated due to -Wfatal-errors

If the constant pointer is initialised as shown below, it would work. 

#include <iostream>
using namespace std;
int main ()
{
int c = 15;
int *const bar = &c; // constant pointer to int
cout << "Address of bar: " << bar << " Value: " << *bar << endl;
*bar = 16; // the value of c can be changed through the pointer.
cout << "bar holds value 16 now!! " << endl
<< "Address of bar: " << bar << " Value: " << *bar << endl << endl;
return 0;
}

 

Output:1
2
3
4
Address of bar: 0xfff2c548 Value: 15
bar holds value 16 now!!
Address of bar: 0xfff2c548 Value: 16

Pointer to Constant (int const *)

  • Pointer to constant defines that the value is constant. This indicates that the pointer can be changed or we can change where the pointer points to. 
  • If the const keyword appears left side of *, the pointed data is constant. 
#include <iostream>
using namespace std;
int main ()
{
int a = 5, b = 10, c = 15;
const int *var; // pointer to constant int.
var = &a; // assignment to where var points to.
cout << "Address of var: " << var << " Value: " << *var << endl;
//*var = 6; //error: assignment of read-only location ‘* var’
var = &b; // the pointer var can be changed.
cout << "var points to b now!! " << endl
<< "Address of var: " << var << " Value: " << *var << endl << endl;
int *const bar = &c; // constant pointer to int
// note, you actually need to set the pointer
// here because you can't change it later ;)
cout << "Address of bar: " << bar << " Value: " << *bar << endl;
*bar = 16; // the value of c can be changed through the pointer.
cout << "bar holds value 16 now!! " << endl
<< "Address of bar: " << bar << " Value: " << *bar << endl << endl;
//bar = &a; //error: assignment of read-only variable ‘bar
return 0;
}

 

Output:1
2
3
4
5
6
7
8
Address of var: 0xfffdd6c4 Value: 5
var points to b now!!
Address of var: 0xfffdd6c0 Value: 10
Address of bar: 0xfffdd6bc Value: 15
bar holds value 16 now!!
Address of bar: 0xfffdd6bc Value: 16

Read reference vs pointer in C++ here.

posted on   yusisc  阅读(4)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示