How to use const with Pointers in C++?
Last Updated : 16 May, 2024
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 in C++.
const Qualifier with Pointers in C++
In C++, the const
keyword can be used with pointers in different ways to specify whether the pointer itself, the data it points to, or both should be constant.
1. Pointer to Constant Data
Declaring the pointer to constant data ensures that the data pointed to by the pointer cannot be changed through this pointer, we can change the pointer to point to a different location, but cannot change the data at the location it points to.
Syntax to Declare Pointer to Constant Data
To make the data pointed by the pointer constant, use the below syntax:
const dataType* pointerName;
Example of Pointer to Constant
C++ // C++ program to demonstrate to use pointer to const data #include <iostream> using namespace std; int main() { int value = 5; // creating a pointer to constant const int* ptr = &value; int anotherValue = 10; cout << "Value: " << *ptr << endl; //*ptr = 10; // Error: cannot modify the value pointed // by ptr ptr = &anotherValue; // Allowed: pointer can be // reassigned cout << "Another Value: " << *ptr << endl; }
OutputValue: 5 Another Value: 10
2. Constant Pointer
Declaring a constant pointer means, the pointer itself is constant and we cannot change what it points to after its initialization, but we can modify the data at the location it points to.
Syntax to Declare Constant Pointer
To make the pointer itself constant, use the below syntax:
dataType* const pointerName;
Example of Constant Pointer
C++ // C++ program to demonstrate how to create a constant // pointer #include <iostream> using namespace std; int main() { int value = 5; int anotherValue = 10; int* const ptr = &value; cout << "Initial Value: " << *ptr << endl; *ptr = 10; // Allowed: modifying the value pointed by ptr cout << "Modified Value: " << *ptr << endl; // ptr = &anotherValue; // Error: ptr is a const pointer // and cannot be reassigned }
OutputInitial Value: 5 Modified Value: 10
3. Constant Pointer to Constant Data
Declaring constant pointer to constant data combines the restrictions of the previous two i.e. neither the pointer can point to a different address, nor the data it points to can be changed through it.
Syntax to Declare Constant Pointer to Constant Data
To make both the pointer and the data it points to constant, use the following syntax:
const dataType* const pointerName;
Example of Constant Pointer to Constant Data
C++ // C++ program to demonstrate how to use Constant Pointer to // Constant Data #include <iostream> using namespace std; int main() { int value = 5; const int* const ptr = &value; cout << "value: " << *ptr << endl; //*ptr = 10; // Error: cannot modify the value // // pointed by ptr // ptr = &anotherValue; // Error: ptr is a const pointer // and cannot be reassigned }
Similar Reads
How to Use const_iterator with a Map in C++? In C++, a const_iterator is a type of iterator that points to constant content. This means that using a const_iterator, you can read from but not write to the element it points to. In this article, we will learn how to use a const_iterator with a map in C++ STL. Example: Input: myMap = {{âappleâ, 1}
2 min read
How to Traverse a Set with const_iterator in C++? In C++, sets are a type of associative container in which each element has to be unique because the value of the element identifies it. It contains a constant iterator that provides the constant reference to its elements. In this article, we will discuss how to traverse a set with const_iterator in
2 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
How to Use Const Keyword in C++? In C++, the const keyword is used to declare constants or unchangeable values. It indicates an immutable object that cannot be modified. The const keyword can be applied to variables, pointers, member functions, objects, and references. In this article, we will learn how to use the const keyword in
4 min read
How to Create Vectors of Unique Pointers in C++? In C++, the vector is defined as a dynamic array that can grow or shrink in size. Vectors of unique pointers are commonly used to manage the collections of dynamically allocated objects as they combine the dynamic resizing capability of vectors with the automatic memory management provided by unique
2 min read