Different ways to use Const with Reference to a Pointer in C++ Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: CPP // C++ program to // demonstrate a Pointer #include <iostream> using namespace std; int main() { // Variable int i = 10; // Pointer to i int* ptr_i = &i; cout << *ptr_i; return 0; } Output: 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: CPP // C++ program to // demonstrate a Reference #include <iostream> using namespace std; int main() { // Variable int i = 10; // Reference to i. int& ref = i; // Value of i is now // changed to 20 ref = 20; cout << i; return 0; } Output: 20 References to pointers is a modifiable value that's used same as a normal pointer. datatype *&var_name; Example 1: CPP // C++ program to demonstrate // References to pointers #include <iostream> using namespace std; int main() { // Variable int i = 10; // Pointer to i int* ptr_i = &i; // Reference to a Pointer ptr_i int*& ptr_ref = ptr_i; cout << *ptr_ref; return 0; } Output: 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 CPP // C++ program to demonstrate // References to pointers #include <iostream> using namespace std; int main() { // Variable int i = 10; int j = 5; // Pointer to i int* ptr = &i; // Reference to a Pointer ptr int*& ptr_ref = ptr; // Trying to change the reference // to Pointer ptr_ref to address of j ptr_ref = &j; cout << *ptr; return 0; } Output: 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: CPP // C++ program to demonstrate // References to pointers #include <iostream> using namespace std; int main() { // Variable int i = 10; int j = 5; // Pointer to i int* ptr = &i; // Const Reference to a Pointer int* const& ptr_ref = ptr; // Trying to change the const reference // to Pointer ptr_ref to address of j ptr_ref = &j; cout << *ptr; return 0; } Compilation 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: CPP // C++ program to demonstrate // References to pointers #include <iostream> using namespace std; int main() { // Variable int i = 10; int j = 5; // Pointer to i int* ptr = &i; // Const Reference to a Pointer int* const& ptr_ref = ptr; // Trying to change the reference // to Pointer ptr_ref *ptr_ref = 100; cout << *ptr; return 0; } Output: 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: CPP // C++ program to demonstrate // References to pointers #include <iostream> using namespace std; int main() { // Variable int i = 10; int j = 5; // Const Pointer to i int const* ptr = &i; // Reference to a Const Pointer int const*& ptr_ref = ptr; // Trying to change the value of the pointer // ptr with help of its reference ptr_ref *ptr_ref = 124; cout << *ptr; return 0; } Compilation 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. Comment More infoAdvertise with us Next Article Passing Reference to a Pointer in C++ S soham1234 Follow Improve Article Tags : C++ Programs C++ cpp-references cpp-pointer C++-const keyword Advanced Pointer +2 More Practice Tags : CPP Similar Reads How to use const with Pointers in C++? In C++, the const keyword is used as a type qualifier for defining read-only (immutable) objects that cannot be modified anywhere in their lifetime. It can be used in several ways with pointers, each serving a different purpose. In this article, we will learn how to use const qualifier with pointers 3 min read Overloads of the Different References in C++ This article focuses on function/method overloads by references, as well as the types of arguments that can be passed. Prerequisites: l-value references.r-value references.Move semantics - std::move(). Overview:l-value refers to a memory location that identifies an object. r-value refers to the data 12 min read Passing Reference to a Pointer in C++ Prerequisite: Pointers vs References in C++. For clear understanding, let's compare the usage of a "pointer to pointer" VS "Reference to pointer" in some cases. Note: It is allowed to use "pointer to pointer" in both C and C++, but we can use "Reference to pointer" only in C++. Passing pointer to a 4 min read Passing Reference to a Pointer in C++ Prerequisite: Pointers vs References in C++. For clear understanding, let's compare the usage of a "pointer to pointer" VS "Reference to pointer" in some cases. Note: It is allowed to use "pointer to pointer" in both C and C++, but we can use "Reference to pointer" only in C++. Passing pointer to a 4 min read How to Traverse a List with const_iterator in C++? In C++, a list is a container used to store data in non-contiguous memory locations. It also provides a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a list with const_iterator in C++. Example Input: myList = {10,20,30,40,50} 2 min read Why Const is Used at the End of Function Declaration in C++? In C++, the const keyword is used to define the constant values that cannot be changed during the execution of the program. Once a value or a function is declared as constant then its value becomes fixed and cannot be changed and if we try to change it, an error is thrown. In this article, we will l 2 min read Like