Pointers/Memory Allocation   «Prev  Next»
Lesson 3 Creating const pointer arguments to functions
Objective Examine use of keyword const to create pointer arguments

Creating const pointer Argument Declarations to Functions

In C++, the keyword `const` is used to indicate that a function parameter, particularly a pointer, should not be used to modify the data it points to. This ensures that the function can read the data pointed to by the pointer, but cannot change it. Using `const` with pointers in function arguments can be done in two primary ways:
  1. Pointer to Constant Data: This declares that the data being pointed to will not be modified by the function. The syntax is `const Type* pointerName`, where `Type` is the data type of the variable the pointer points to.
  2. Constant Pointer: This declares that the pointer itself cannot be changed to point to a different memory location, although the data it points to can be modified. The syntax is `Type* const pointerName`.
    Here's an example demonstrating both usages in the context of a function:
    #include <iostream>
    
    // Function with a pointer to constant data
    void printArray(const int* arr, int size) {
        for (int i = 0; i < size; ++i) {
            std::cout << arr[i] << " ";
            // arr[i] = i; // This line would cause a compilation error
        }
        std::cout << "\n";
    }
    
    // Function with a constant pointer
    void incrementAll(int* const arr, int size) {
        for (int i = 0; i < size; ++i) {
            arr[i]++;
        }
        // arr = nullptr; // This line would cause a compilation error
    }
    
    int main() {
        int numbers[] = {1, 2, 3, 4, 5};
        int size = sizeof(numbers) / sizeof(numbers[0]);
    
        printArray(numbers, size); // Prints the array elements
        incrementAll(numbers, size); // Increments each element by 1
        printArray(numbers, size); // Prints the modified array elements
    
        return 0;
    }
    

In `printArray`, `const int* arr` means the function can use `arr` to read the array elements but cannot modify them. In `incrementAll`, `int* const arr` means the function can modify the elements pointed by `arr` but cannot change the `arr` pointer to point elsewhere. This usage of `const` improves code readability and safety, indicating the intended use of the pointers to other developers and to the compiler.


const type * identifier

declares the identifier as a pointer whose pointed at value is constant. This construct is used when pointer arguments to functions will not have their contents modified.
So
void fcn(const int* p){
  // within here it is illegal
  // to have  *p = expression;
}

This provides "const safety" allowing the compiler to detect an error. It also allows the compiler to optimize how these arguments are passed.

Non-const pointers

Let us contrast this with a non-const pointer argument:
void fcn(int* p){
 // within here it is legal
 // to have  *p = expression;
}

Now attempting to pass a const pointer value will lead to a syntax error.
const int size = 100;  //size may not be modified
fcn(&size);  //illegal because the address
  // of a const variable is being passed to an
  // argument that allows for modification
  // of what is pointed at.

Declaring a pointer constant

We can take this one step further. The form
const type* const identifier

declares the identifier as a
pointer constant.

Examples

Here are some other examples of const variable declaration.

SEMrush Software