none
What are the differences between const int*, int * const, and const int * const?

Answers

  • Const usage

    Meaning

    Description

    const int *x;

    Pointer to a const int

    Value pointed to by x can’t change

    int * const x;

    Const pointer to an int

    x cannot point to a different location.

    const int *const x;

    Const pointer to a const int

    Both the pointer and the value pointed to cannot change.

    Monday, June 28, 2010 2:24 AM

All replies

  • Const usage

    Meaning

    Description

    const int *x;

    Pointer to a const int

    Value pointed to by x can’t change

    int * const x;

    Const pointer to an int

    x cannot point to a different location.

    const int *const x;

    Const pointer to a const int

    Both the pointer and the value pointed to cannot change.

    Monday, June 28, 2010 2:24 AM
  • What are the differences between const int, int * const, and const int * const?*

    Two "tricks" that can help:

    1. You can swap the const with the data type to move the const to the right
       (a bit of care should be taken not to swap too much :-)
    2. Read from right

      const int *
          1. => int const *
          2. => pointer to a const int
          so the pointer "points" to an int that can't be changed

      int * const
          2. => const pointer to int
          so the pointer "points" to an int that can be changed,
          but the pointer can't change

      const int * const
          1. => int const * const
          2. => const pointer to const int
          constant pointer (can't change) points to an int that you can't change

    -- Mihai Nita [Microsoft MVP, Visual C++]
    http://www.mihai-nita.net
    ------------------------------------------
    Replace year with _ to get the real email

    Monday, June 28, 2010 5:41 AM
  • What are the differences between const int*, int * const, and const int * const?

    The C++ FAQ Lite has a nice discussion of this:

    http://www.parashift.com/c++-faq-lite/const-correctness.html

    In particular, question 18.5.

    Monday, June 28, 2010 4:14 PM
  • 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 be on either side of the type so:

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

    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

    Tuesday, June 29, 2010 11:16 AM