const pointer versus pointer to const

People new to C/C++ are sometimes confused about the difference between a const pointer and a pointer to const. A const pointer essentially means you can’t change the pointer variable itself, but you can change the value it points to. A pointer to const means you can change the pointer but not what it points to. You can use them both together and have a const pointer to a const. The code snippet below should make it really clear I hope.

//pointer to a const
void f1()
{
    int i = 100;
    const int* pi = &i;
    //*pi = 200; <- won't compile
    pi++;
}

//const pointer
void f2()
{
    int i = 100;
    int* const pi = &i;
    *pi = 200;
    //pi++; <- won't compile
}


//const pointer to a const
void f3()
{
    int i = 100;
    const int* const pi = &i;
    //*pi = 200; <- won't compile
    //pi++; <- won't compile
}
Advertisements
Posted in C++

8 thoughts on “const pointer versus pointer to const

  1. MeThink read-only objects should not use the confusing const, but rather a [Read-Only] attribute that makes things particularly clearer, at least it fixes the prefix versus suffix problem.
    I wonder what you think about the MFC7 being merged with STL and as such full of const everywhere, unlike previous MFC releases. Is that a way to get developers disgusted enough of it? Eh.

  2. Hello Stephane

    I believe it was MFC7 and ATL7 that got merged, not STL; unless I got my facts really wrong. 😮

    Or was it a typo on your part?

  3. NO it wasn’t a typo, ATL has been rewritten in part using STL, and then the whole thing got merged with MFC. Regardless the viewpoint, the result is the same, MFC headers full of const stuff with the following consequences : 1) much harder to debug, even the CString class is now a typedef from a template 2) breaking changes with earlier MFC releases : for instance, if you used afxcoll.h and the like, bad luck, it’s very likely you have to make substantial changes in your code and API when migrating to VC7.
    I haven’t taken a look at the MFC release that comes along with Whidbey (they call this a beta even though a bunch of features are still not implemented and popup that kind of message).

    Makes me think that whoever writes a book for targeted to MFC programmers will hit the street. I am not talking about the book you wrote with TA, rather am talking about a real migration path for MFC programmers. Example : where is ON_UPDATE_CMDUI gone? …

    By the way, nisho, how is the book selling doing? 🙂

  4. Ah okay. I myself found the templated CString a little confusing when I was looking up MSDN for a CString method 🙂

    The book didn’t do as well as Tom and I expected 😦 And I haven’t got the sales reports from Addison Wesley as yet, so I don’t even know how many books have been sold.

    BTW, don’t see you on CP very much these days. What’s cooking, Stephane?

  5. “Ah okay. I myself found the templated CString a little confusing when I was looking up MSDN for a CString method”
    That’s your usual being nice at it. I have refrained from using those in total disgust and wonder how they could make things harder to use. Well, by obfuscating the code may be.

    “The book didn’t do as well as Tom and I expected”
    It’s a little surprising given how few books there are on the subject.

    “don’t see you on CP very much these days”
    Well ya know, the site has been a little too-much-of-everything lately. My full-time job has certainly something to do on top of that and while I am writing software that I try to sell along with a blog, I will probably start a project sometimes in the future : it’s about vulnerability diagnosis of IE : windows XP SP2 is for…XP guys. If you know people interested in this, let me know. 😉

  6. well i knew it before
    and i feel that the more we know about pointers ,there is mu..ch more.These kind of questions appear in almost all placement exams .

  7. The difference between the const pointer and pointer to a constant is very good ie clear cut to the exact definition .Can you put more such examples in your website which wll be helpful to begineers like me who are new C++ programming?

Leave a Reply

Fill in your details below or click an icon to log in:

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