About const pointers

There are two different ways to use const pointers:

const int *p; // the same as int const *p

and

int * const p;

In the first, you can’t change the content of the address you point to. In the second, you can’t change the address you point to. Example:

    int a, b;
    int * const p(&a);

    p = &b; // error: trying to change address
    *p = 3; // ok
    int a, b;
    const int * p(&a);

    p = &b; // ok
    *p = 3; // error: trying to change content of address

Reference: http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-int-const

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s