Use const Qualifier With Pointers in C++

  1. Use the const type var Notation to Declare Read-Only Object in C++
  2. Use the const Qualifier With Pointers to Handle Read-Only Objects in C++

This article will demonstrate multiple methods about how to use a const qualifier with pointers in C++.

Use the const type var Notation to Declare Read-Only Object in C++

The C++ provides the keyword const as the qualifier for objects that need to be defined as read-only (immutable). const variables are declared with the notation const type var or type const var, both of which are syntactically correct, but the former one is used as conventional style. Since const qualified objects are not mutable, they must be initialized during the declaration. This makes a statement const int number; - invalid and throws compiler error (probably your IDE will also scream about it).

When the const variable is initialized, it can not be assigned a different value during run-time. Thus, the third line in the following example’s main function is invalid, and the compiler won’t process it. Note that if you declare a pointer to the same type of a variable and then try to assign the address of the const variable to it, the error is reported by the compiler. Note that the latter error is usually overridden if we compile with the -fpermissive flag.

#include <iostream>

int main() {
    const int number = 1234;

    number = 235; //Error
    int *ptr = &number; // Error

    return 0;
}

Use the const Qualifier With Pointers to Handle Read-Only Objects in C++

The const qualifier is often used with pointers. There are three types of declarations const type * var, type *const var and const type *const var. The first declares the var pointer to read-only type object, meaning that the object can’t be modified but the pointer itself can be. The second - var read-only pointer to type object, where we declare unique, an immutable pointer to the object that can be modified, and the last one defines the both - pointer and the object as immutables.

These notations provide multiple useful features that are explored in the following code samples. As shown in the last example, we couldn’t store the address of the const variable in a non-const pointer, but if we add the const specifier, the operation is valid. Mind though, we still can’t modify stored value via newly declared pointer as demonstrated in the 4th line of the main loop:

#include <iostream>

using std::cout; using std::endl;

#define STR(num) #num

int main() {
    const int number = 1234;

    const int *c_ptr = &number;
//    *c_ptr = 42; // Error

    cout << STR(number) << " - " << number << endl;
    cout << STR(*c_ptr) << " - " << *c_ptr << endl;

    return 0;
}

Output:

number - 1234
*c_ptr - 1234

Another common issue while using the const qualifier with pointers is non-const pointers’ assignment to the pointers that point to the read-only objects. Notice that, there’s a new non-const variable number2 initialized in the next code example, and the c_ptr that was declared as a pointer to const object, is now assigned with the address of number2. This operation is legal in C++, and the consequence is that we can only read the value stored in the number2 variable via c_ptr, but any modifications will result in a compiler error.

#include <iostream>

using std::cout; using std::endl;

#define STR(num) #num

int main() {
    const int number = 1234;
    const int *c_ptr = &number;

    int number2 = 3456;
    c_ptr = &number2;
//    *c_ptr += 12; // Error
    number2 += 12;

    cout << STR(number) << " - " << number2 << endl;
    cout << STR(*c_ptr) << " - " << *c_ptr << endl;

    return 0;
}

Output:

number2 - 3468
*c_ptr - 3468

Related Article - C++ Pointer

  • Check if Pointer Is NULL in C++