Search

8.9 — Pointers and const

So far, all of the pointers you’ve seen are non-const pointers to non-const values.

The following code snippet works fine:

However, what happens if value is const?

The above snippet won’t compile -- we can’t set a (non-const) pointer to a const variable. This makes sense: a const variable is one whose value can not be changed. Hypothetically, if we could set a non-const pointer to a const value, then we would be able to dereference the non-const pointer and change the value. That would violate the intention of const.

Pointer to const value

A pointer to a const value (sometimes called a pointer to const for short) is a (non-const) pointer that points to a constant value.

To declare a pointer to a const value, use the const keyword before the data type:

In the above example, ptr points to a const int.

So far, so good, right? Now consider the following example:

Similar to a reference to const, a pointer to const can point to non-const variables too. A pointer to const treats the variable as constant when it is accessed through the pointer, regardless of whether the variable was initially defined as const or not.

Thus, the following is okay:

But the following is not:

Because a pointer to const is not const itself (it just points to a const value), the pointer can be redirected to point at other values:

Const pointers

We can also make a pointer itself constant. A const pointer is a pointer whose value can not be changed after initialization

To declare a const pointer, use the const keyword between the asterisk and the pointer’s name:

Just like a normal const variable, a const pointer must be initialized to a value upon declaration. This means a const pointer will always point to the same address. In the above case, ptr will always point to the address of value (until ptr goes out of scope and is destroyed).

However, because the value being pointed to is still non-const, it is possible to change the value being pointed to via dereferencing the const pointer:

Const pointer to a const value

Finally, it is possible to declare a const pointer to a const value by using the const keyword both before the type and before the variable name:

A const pointer to a const value can not be set to point to another address, nor can the value it is pointing to be changed through the pointer.

Pointer and const recap

To summarize, you only need to remember 4 rules, and they are pretty logical:

  • A non-const pointer can be redirected to point to other addresses.
  • A const pointer always points to the same address, and this address can not be changed.
  • A pointer to a non-const value can change the value it is pointing to. These can not point to a const value.
  • A pointer to a const value treats the value as const when accessed through the pointer, and thus can not change the value it is pointing to.

Keeping straight the declaration syntax can be challenging. Just remember that the type of value the pointer points to is always on the far left:


8.10 -- Null pointers
Index
8.8 -- Pointers

24 comments to 8.9 — Pointers and const

  • papagym177

    If (int *const pnPtr = &nValue;) and
    (const int *pnPtr = &nValue;) are possible

    Can we use (int const * pnPtr = &nValue;)as well?
    Thanks

  • kekie

    This seems to be a very silly naming system. Shouldn't 'a pointer to a constant variable' be called a 'read only pointer' or something?

  • 1krishna23

    hai
    this site very use full and can i know
    1.difference between free() and exit().
    2.on exit() dynamic memory will be freed with out using free()

    int main()
    {
    char *ptr=(char*)malloc(100);
    ptr +=50;
    free(ptr);
    printf("hai\n ");
    exit(0);
    }

  • sumit agrawal

    #include

    int main()
    {
    using namespace std;
    int nvalue=10;
    int nvalue2=5;
    const int *ptr=&nvalue;
    nvalue=20;
    cout << *ptr << endl;
    cout << nvalue << endl;
    return 0;
    }

    output:
    20
    20

    why so..?? the value of nvalue should be constant to 10 if we access it through *ptr but here its changing to 20 even if we are accessing it through *ptr...may b m lacking in understanding the concept...still can some1 plz clarify me dis...

  • alwaysAstudent

    Please correct me if I am wrong. I think that the relation between pointers and constant can be best understood when seen from pointers' point of view.

    CONSTANT POINTER : is a pointer which is constant; meaning it holds a constant value (address which it points to). that is, it constantly points to the address of a variable (assigned during declaration). the variable may not be a constant. thus the constant pointer points to one unique address and can manipulate the variable that has that address. hence,

    int nValue = 5;
    int *const pnPtr = &nValue;
    *pnPtr = 6; // it is possible !!
    nValue = 7; // this is also possible !! inadvertently skipped in this tutorial i guess

    POINTER TO A CONSTANT VARIABLE :

    the variable may not be a constant but still, when declared this way, the pointer treats the variable as a constant.

    int nValue = 5;
    const int *pnPtrt = &nValue; // POINTER TO A CONSTANT VARIABLE
    nValue = 6; // this can be done as the variable need not be constant

    however, *pnPtr = 7; will cause program to crash because the pointer pnPtr treats nValue as a constant.

    CONSTANT POINTER TO A CONSTANT VARIABLE :

    here, both the pointer as well as the variable, are constant.

    const int nValue = 5;
    const int *const pnPtr = &nValue;

    nValue = 6; // not possible because nValue is a constant
    *pnPtr = 6; // not possible because pnPtr is pointer to a constant ( and not because pnPtr is a constant pointer !)

  • MidnightRider

    So far i uderstand the pointers but i need to practice,any help ?

  • Sultan

    Hi there,

    I don't understand what the benefit is when we declare a pointer as constant.

    Any help?

  • hypehuman

    Dear Alex,

    This tutorial is awesome! As I am going through it, I am building a program that will play Minesweeper by itself. It works right now, but I am modifying it daily to apply the good programming practices I am learning here.

    Anyway, I see you typed: "a const pointer will always point to the same value".
    Did you mean: "a const pointer will always point to the same location"?

    Maybe I'm just not understanding it.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">