The keyword const with pointers

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. Constant variables

    /

  5. The keyword const with pointers

Constant pointers

Here, it is a bit more complex. We can define constant pointers, pointers to a constant variable and constant pointers to a constant variable. To define a constant pointer, we must add the keyword const in the definition of the pointer, between the asterisk * and the name of the pointer. Example of a definition of a constant pointer:

int var = 57; int var2 = 3; int * const ptr = &var;

In the example above, the pointer ptr is constant, so we could not change its value. That means we could not make it point to an other variable (Could not change the memory address it stores). The following line would then cause an error:

ptr = &var2;

However, since it is not a pointer to a constant variable, we could do this:

*ptr = 6; // var now equals 6

Pointers to a constant variable

To define a pointer to a constant variable we have to add, in its definition, the keyword const before the asterisk *. Example:

int var = 57; int var2 = 3; const int * ptr = &var;

Now, in the example above, we can not change the value of the variable pointed by ptr. So the following line would cause an error:

*ptr = 5;

However, since the pointer ptr is not constant, we can change which variable it points to, so the code below works fine:

ptr = &var2; // ptr now points to var2 (Stores the memory address of var2).

We can also retrieve the value of the variable pointed by ptr:

int copy = *ptr;

It is also possible to define a pointer to a constant variable by putting, in its definition, the keyword const after the type of the pointer (As long as it is before the asterisk *). Example:

int const * ptr = &var;

Note that a pointer to a constant variable does not have to point to a variable that is constant. However, a variable that is constant can not be pointed by a pointer that points to a non-constant variable (Otherwise its value could be changed through the pointer). The following example would cause an error:

const int CST = 78; int * ptr = &CST; /* Error: ptr is not a pointer that points to a constant location so it can not point to the constant variable CST. */

Constant pointers to a constant variable

To define a constant pointer to a constant variable, we must add, in the pointer definition, the keyword const two times. One before the asterisk *, to make it a pointer to a constant variable and one after the asterisk to make it a constant pointer. Example:

int var = 57; int var2 = 3; const int * const ptr = &var;

In the example above, we can not change the value of the variable pointed by ptr nor make ptr point to an other variable. As expected, the following two lines would both cause an error:

ptr = &var2; *ptr = 0;

Functions returning a constant pointer

Making a pointer, returned by a function, constant is done the same way as with references: We add the keyword const, in the function definition, before or right after the name of the return type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> const int* pointerOf(int& arg) { return &arg; } int main() { int a = 43; // Retrieves the constant address returned by the function. const int *ptr = pointerOf(a); /* Since the pointer points to a constant variable, we can not change the value of the pointed variable. We can only retrieve its value. */ std::cout << *ptr << std::endl; return 0; }

Since the function pointerOf, from the example above, returns a constant address (pointer), it can only be assigned to a pointer that points to a constant variable. It therefore ensures that the value of the variable located the address returned will not be altered.

Note that adding the keyword const after the type is also valid:

int const* pointerOf(int& arg) { return &arg; }