Open In App

How to use const with Pointers in C++?

Last Updated : 16 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
Value: 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
}

Output
Initial 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
}

Output
value: 5

Next Article
Practice Tags :

Similar Reads