Distinguishing between const pointers

Source: Internet
Author: User

Read it backwards ...

    • int*-Pointer to int
    • int const *-Pointer to const INT
    • int * const-Const pointer to int
    • int const * const-const pointer to const INT

Now the first const can is on either side of the type so:

    • const int *==int const *
    • const int * const==int const * const

If you want to go really crazy you can do things like this:

    • int **-pointer to pointer to int
    • int ** const-A const pointer to a pointer to an int
    • int * const *-A pointer to a const pointer to an int
    • int const **-A pointer to a pointer to a const INT
    • int * const * const-A const pointer to a const pointer to an int
    • ...

And to make sure we is clear on the meaning of const

  const  Int* Foo;int *const Bar //note, you actually need to set the pointer  / /here because you can ' t change it later;)           

foois a variable pointer to a constant int. This is lets you a change, and the value of that. Most often this is seen with cstrings where you have a pointer to a const char . You could change which string of your point to if you can ' t change the content of these strings. This was important when the string itself was in the data segment of a program and shouldn ' t be changed.

baris a const or fixed pointer to a value that can changed. This is a reference without the extra syntactic sugar. Because of this fact, usually your would use a reference where you would use a T* const pointer unless you need to allow null Pointers.

Distinguishing between const pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.