Const Pointer in C++

In this lecture, we discuss about Const pointer and Pointer to Const and Const Pointer to Const in C++.

Why do we need Const Pointer or Pointer to Const?

A pointer is a variable that stores the memory address of another variable or the memory address of the memory allocated on the heap. For example,

int num = 11;

// Integer pointer pointing 
// to the address of num
int * ptr = #

Here, we have created a variable ‘num’ and initialized it with the value 11, then created a pointer ‘ptr’ and initialized it with the address of variable ‘num’. Now, the pointer ‘ptr’ contains the memory location where the value of the variable ‘num’, i.e. 11, is stored.

You can access this value using the pointer ‘ptr’ by dereferencing it. Like this,

std::cout<< *ptr; 

Output:

11

You can also change the value at that memory location. For example, you could change the value at the memory location pointed to by ‘ptr’ to 20, which in turn would change the value of variable ‘num’. You can print and confirm that the value of the variable ‘num’ is now 20.

*ptr = 20;

std::cout<< num << std::endl;

Output:



    20
    

    This is how you change the value at a memory location using a pointer.

    Now, suppose you pass a pointer to a function and, inside the function, someone changes the value. This value change inside the function using the pointer will be reflected outside the function as well. For example, if you have a variable ‘num’, initialized with the value 11, and then create a pointer ‘ptr’ containing the address of this variable, if you then pass this pointer to the function ‘getSquare’, this function is supposed to return the square of the value at the memory location pointed to by the given pointer. But if a new developer comes along and tries to change the value at this location pointed to by the pointer, because you passed a pointer, he can directly change the value.

    #include <iostream>
    
    int getSquare(int* ptr)
    {
        *ptr = 2;
    
        return (*ptr) * (*ptr);
    }
    
    int main()
    {
        int num = 11;
    
        // Integer pointer pointing 
        // to the address of num
        int * ptr = &num;
    
        int square = getSquare(ptr);
    
        std::cout<< square << std::endl; 
    
        std::cout<< "num = " << num << std::endl; 
    
        return 0;
    }
    

    Output:

    4
    

    We wanted the square of 11, but inside the function value at memory location got changed to 2, and it returned the square of 2 instead. Also after the function getSquare() returns, the value of num is also changed to 2.

    Pointer to Const in C++

    You can restrict the user from changing the value pointed to by the pointer by using a pointer to const. While creating a pointer, you can make the pointer point to a const integer. In both cases, the pointer ‘ptr’ will be pointing to a memory location, but it cannot change the data at that memory location. So, if someone tries to change the value now, it will give an error. Similarly, if you want to pass a pointer to a function for read operation only, then you can pass it like this.

    int getSquare(const int* ptr)
    {
        *ptr = 2;
    
        return (*ptr) * (*ptr);
    }
    

    Here, you are passing a pointer pointing to a const integer, so nobody can change the value at that memory location using the pointer. This is called pointer to const.

    int num = 11;
    
    // Integer pointer pointing 
    // to the address of num
    int * ptr = &num;
    
    int square = getSquare(ptr);
    
    std::cout<< square << std::endl; 
    
    std::cout<< "num = " << num << std::endl; 
    

    Error:

    example1.cpp: In function ‘int getSquare(const int*)’:
    example1.cpp:5:10: error: assignment of read-only location ‘* ptr’
        5 |     *ptr = 2;
    

    Even if you restricted the user so that they cannot change the value at the memory location pointed to by the pointer ‘ptr’, the pointer itself is also a variable, so you can change it to point to a new memory location. Check out this example: here, inside the ‘getSquare()’ function, we change the value of the pointer ‘ptr’ and make it point to a new memory location.

    #include <iostream>
    
    int getSquare(const int* ptr)
    {
        ptr = new int(2);
    
        return (*ptr) * (*ptr);
    }
    
    int main()
    {
        int num = 11;
    
        // Integer pointer pointing 
        // to the address of num
        int * ptr = &num;
    
        int square = getSquare(ptr);
    
        std::cout<< square << std::endl; 
    
        return 0;
    }
    

    Output:

    4
    

    Const Pointer in C++

    Is there any way to restrict the user so that they can’t change the pointer to point to a new memory location? Answer is Yes, by making pointer a constant pointer.

    To restrict the user so they cannot make the pointer point to another memory location, you need to make the pointer const, like this.

    int getSquare(int* const ptr)
    {
        *ptr = 4; // No Error
    
        ptr = new int(2); // Error
    
        return (*ptr) * (*ptr);
    }
    

    Here, your pointer is a const pointer pointing to a memory location, or the address of variable ‘num’. Now, even if you want to point the pointer to a new memory location, you can’t do that because the pointer is of const type. However, using the pointer, you can change the value at that memory location, as the pointer is not pointing to a constant integer. This is different from the previous scenario.

    Const pointer pointing to Const in C++

    In this section, we will cover a combination of both: a const pointer pointing to a constant integer. Suppose you initialize the variable ‘num’ with the value 11. Now, you want to create a pointer to this but you want to restrict it so that no one can change the value of ‘num’ using the pointer, and also, once you initialize the pointer to the address of ‘num’, no one can make that pointer point to any other location. For that, you have to use a const pointer pointing to a const value, like this.

    int getSquare(const int* const ptr)
    {
        *ptr = 2; // Error
        ptr = new int(2); // Error
    
        return (*ptr) * (*ptr);
    }
    

    Here, your pointer ‘ptr’ is a const pointer and is pointing to a constant value. So, we cannot make the pointer point to any other location, and we also cannot use the pointer to change the value at that memory location.

    Const pointer vs Pointer to Const vs Const Pointer to Const

    Sure, here’s a simple table to illustrate the differences:

    • Const Pointer:
      • A const pointer means that the memory location that the pointer is pointing to can be changed, but the pointer itself cannot change and point to a different memory location. In other words, the pointer is constant, but the data at which it points is not.
    • Pointer to Const:
      • A pointer to const means that the pointer can point to different locations, but the data at the memory location the pointer is pointing to cannot be changed using this pointer. In other words, the data is constant but the pointer is not.
    • Const Pointer to Const:
      • A const pointer to const combines both concepts – the pointer cannot point to a different memory location and the data at the memory location cannot be changed. Both the pointer and the data it points to are constant.

    Summary

    In this lecture, we learned about Const pointer and Pointer to Const and Const Pointer to Const in C++.

    Scroll to Top