const against non const pointer

Discussion in 'C++' started by mathon@gmx.at, Nov 7, 2006.

  1. Guest

    Hello,

    if i have these two declarations:

    1)const int * test;

    and

    2)int* const test;


    Does the first mean that the value of the integer cannot be changed and
    the second one that the location to which the pointer points to cannot
    be changed...? or is the other way around?

    matti
    , Nov 7, 2006
    #1
    1. Advertising

  2. Noah Roberts Guest

    wrote:
    > Hello,
    >
    > if i have these two declarations:
    >
    > 1)const int * test;
    >
    > and
    >
    > 2)int* const test;
    >
    >
    > Does the first mean that the value of the integer cannot be changed and
    > the second one that the location to which the pointer points to cannot
    > be changed...? or is the other way around?


    You have it right.
    Noah Roberts, Nov 7, 2006
    #2
    1. Advertising

  3. mlimber Guest

    wrote:
    > if i have these two declarations:
    >
    > 1)const int * test;
    >
    > and
    >
    > 2)int* const test;
    >
    >
    > Does the first mean that the value of the integer cannot be changed and
    > the second one that the location to which the pointer points to cannot
    > be changed...? or is the other way around?


    See the FAQ:

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

    Cheers! --M
    mlimber, Nov 7, 2006
    #3
  4. wrote:
    > if i have these two declarations:
    >
    > 1)const int * test;
    >
    > and
    >
    > 2)int* const test;
    >
    >
    > Does the first mean that the value of the integer cannot be changed and
    > the second one that the location to which the pointer points to cannot
    > be changed...? or is the other way around?


    The first variant means that the integer the pointer points to cannot be changed
    through pointer 'test' without an explicit cast. Note that it is still possible
    that the integer can be changed through some other access path (directly or
    through some other pointer). Also note that if the integer itself is not a
    constant object, it is perfectly legal to cast away the constness an change it
    through 'test':

    *const_cast<int*>(test) = 42;

    The second variant means that pointer 'test' cannot be changed (as I understand,
    that what you meant by "the location to which the pointer points to cannot be
    changed").

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 7, 2006
    #4
  5. Mark P Guest

    wrote:
    > Hello,
    >
    > if i have these two declarations:
    >
    > 1)const int * test;
    >
    > and
    >
    > 2)int* const test;
    >
    >
    > Does the first mean that the value of the integer cannot be changed and
    > the second one that the location to which the pointer points to cannot
    > be changed...? or is the other way around?
    >
    > matti
    >


    Others have already confirmed that you are correct. As a tip, these
    sorts of declarations, notably ones with brackets or parentheses, can in
    general be read right to left:

    1) test is a pointer to an int constant
    2) test is a constant pointer to an int

    For more complicated declarations, check out:

    http://www.ericgiguere.com/articles/reading-c-declarations.html
    (it's a bit old and written for C, but the basic ideas still hold)

    Finally, there's a handy little UNIX utility that will convert
    declarations to english and vice versa. Look for cdecl or c++decl.
    Mark P, Nov 7, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. coala
    Replies:
    3
    Views:
    358
    coala
    Sep 6, 2006
  2. Javier
    Replies:
    2
    Views:
    533
    James Kanze
    Sep 4, 2007
  3. fungus
    Replies:
    13
    Views:
    862
    fungus
    Oct 31, 2008
  4. Old Wolf

    Same pointer parameter const and non-const

    Old Wolf, Nov 3, 2009, in forum: C Programming
    Replies:
    3
    Views:
    523
    Ben Bacarisse
    Nov 4, 2009
  5. Jens Thoms Toerring

    Const/non-const pointer returning method

    Jens Thoms Toerring, May 25, 2010, in forum: C++
    Replies:
    11
    Views:
    690
    John H.
    May 26, 2010
Loading...

Share This Page