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 2023-11-30 00:50  yusisc  阅读(1)  评论(0编辑  收藏  举报

导航