Different ways to use Const with Reference to a Pointer in C++
Before moving forward with using const with Reference to a Pointers, let us first see what they are one by one:
- Pointers are used to store the address of variables or a memory location. A variable can be declared as a pointer by putting ‘*’ in the declaration.
datatype *var_name;
Example:
// C++ program to// demonstrate a Pointer#include <iostream>usingnamespacestd;intmain(){// Variableinti = 10;// Pointer to iint* ptr_i = &i;cout << *ptr_i;return0;}chevron_rightfilter_noneOutput:
10
- Reference: When a variable is declared as a reference, it becomes an alternative name for an existing variable. A variable can be declared as a reference by putting ‘&’ in the declaration.
datatype &var;_name;
Example:
// C++ program to// demonstrate a Reference#include <iostream>usingnamespacestd;intmain(){// Variableinti = 10;// Reference to i.int& ref = i;// Value of i is now// changed to 20ref = 20;cout << i;return0;}chevron_rightfilter_noneOutput:20
- References to pointers is a modifiable value that’s used same as a normal pointer.
datatype *&var;_name;
Example 1:
// C++ program to demonstrate// References to pointers#include <iostream>usingnamespacestd;intmain(){// Variableinti = 10;// Pointer to iint* ptr_i = &i;// Reference to a Pointer ptr_iint*& ptr_ref = ptr_i;cout << *ptr_ref;return0;}chevron_rightfilter_noneOutput:10
Here ptr_ref is a reference to the pointer ptr_i which points to variable ‘i’. Thus printing value at ptr_ref gives the value of ‘i’, which is 10.
Example 2: Now let us try to change the address represented by a Reference to a Pointer
// C++ program to demonstrate// References to pointers#include <iostream>usingnamespacestd;intmain(){// Variableinti = 10;intj = 5;// Pointer to iint* ptr = &i;// Reference to a Pointer ptrint*& ptr_ref = ptr;// Trying to change the reference// to Pointer ptr_ref to address of jptr_ref = &j;cout << *ptr;return0;}chevron_rightfilter_noneOutput:5
Here it prints 5, because the value of j is 5 and we changed ptr_ref to point to j. Now as ptr_ref is a reference to pointer ptr, ptr now points to j. Thus we get the output we expected to see.
- Const Reference to a pointer is a non-modifiable value that’s used same as a const pointer.
datatype* const &var;_name;
Example 1:
// C++ program to demonstrate// References to pointers#include <iostream>usingnamespacestd;intmain(){// Variableinti = 10;intj = 5;// Pointer to iint* ptr = &i;// Const Reference to a Pointerint*const& ptr_ref = ptr;// Trying to change the const reference// to Pointer ptr_ref to address of jptr_ref = &j;cout << *ptr;return0;}chevron_rightfilter_noneCompilation Error:
In function 'int main()': prog.cpp:23:13: error: assignment of read-only reference 'ptr_ref' ptr_ref = &j; ^Here we get a compile-time error as it is a const reference to a pointer thus we are not allowed to reassign it.
Example 2:
// C++ program to demonstrate// References to pointers#include <iostream>usingnamespacestd;intmain(){// Variableinti = 10;intj = 5;// Pointer to iint* ptr = &i;// Const Reference to a Pointerint*const& ptr_ref = ptr;// Trying to change the reference// to Pointer ptr_ref*ptr_ref = 100;cout << *ptr;return0;}chevron_rightfilter_noneOutput:100
It prints 100 as it is not a reference to a pointer of a const.
Why Example 2 didn’t throw a Compile-time error when Example 1 did?
- In Example 1, the ptr_ref is a const reference to a pointer to int, and we are trying to change the value of ptr_ref. So the compiler throws a Compile time error, as we are trying to modify a constant value.
- In Example 2, the ptr_ref is a const reference to a pointer to int, and we are trying to change the value of *ptr_ref, which means we are changing the value of int to which the pointer is pointing, and not the const reference of a pointer. So the compiler doesn’t throw any error and the pointer now points to a value 100. Therefore the int is not a constant here, but the pointer is. As a result, the value of int changed to 100.
- Reference to a Const Pointer is a reference to a constant pointer.
datatype const *&var;_name;
Example:
// C++ program to demonstrate// References to pointers#include <iostream>usingnamespacestd;intmain(){// Variableinti = 10;intj = 5;// Const Pointer to iintconst* ptr = &i;// Reference to a Const Pointerintconst*& ptr_ref = ptr;// Trying to change the value of the pointer// ptr with help of its reference ptr_ref*ptr_ref = 124;cout << *ptr;return0;}chevron_rightfilter_noneCompilation Error:
In function 'int main()': prog.cpp:23:14: error: assignment of read-only location '* ptr_ref' *ptr_ref = 124; ^Here again we get compile time error. This is because here the compiler says to declare ptr_ref as reference to pointer to const int. So we are not allowed to change the value of i.
Recommended Posts:
- Passing Reference to a Pointer in C++
- When do we pass arguments by reference or pointer?
- Reference to a pointer in C++ with examples and applications
- Passing by pointer Vs Passing by Reference in C++
- Difference between const char *p, char * const p and const char * const p
- Double Pointer (Pointer to Pointer) in C
- What is a Pointer to a Null pointer
- C++ | const keyword | Question 3
- How to modify a const variable in C?
- C++ | const keyword | Question 5
- C++ | const keyword | Question 5
- C++ | const keyword | Question 1
- C++ | const keyword | Question 2
- Const member functions in C++
- Why copy constructor argument should be const in C++?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

