don't understand this deal with const pointers in this trivial example

Discussion in 'C++' started by johny smith, Jul 6, 2004.

  1. johny smith

    johny smith Guest

    Well,

    I thought that I was creating a constant pointer, and would not be able to
    increment it without a compiler error.

    But this example compiles fine.

    The const is to the left of the * so I thought this would force that to be
    constanst.

    What am I missing? thanks

    #include <iostream>


    void f( int const * a );


    int main()
    {

    int a = 5;

    f( &a );



    return 0;

    }


    void f( int const * b )
    {

    b++;
    }
     
    johny smith, Jul 6, 2004
    #1
    1. Advertisements

  2. johny smith

    John Carson Guest

    It is the opposite. const to the left of * means constancy of the thing
    pointed to (an int in this case). const to the right of * means the pointer
    is constant. You are incrementing the pointer, not the int, which your code
    allows.
     
    John Carson, Jul 6, 2004
    #2
    1. Advertisements

  3. johny smith

    David White Guest

    To decipher this declaration, start at the 'a' and follow any modifiers
    (const or volatile) and operators, in order of precedence, outwards. First
    comes the * operator, so 'a' is a pointer. Next is 'const', so 'a' is a
    pointer to a const. Last is 'int', so 'a' is a pointer to a const int. If
    you want the pointer to be const and not what it points to, change it to:
    int * const a
    If you follow the same process on this, you end up with: const pointer to
    int.
    DW
     
    David White, Jul 6, 2004
    #3
  4. Others have explained your mistake, but maybe your confusion arises from
    ambiguous terminolgy. Constant pointers are pretty rare (usually you would
    use a reference instead), but pointers to constant data are very common,
    therefore people use the term constant pointer to actually mean pointer to
    constant data.

    john
     
    John Harrison, Jul 6, 2004
    #4
  5. johny smith

    David White Guest

    Yes, and they are particularly rare as function parameters. There's not much
    point making any pass-by-value type const because it doesn't matter if you
    change it.
    DW
     
    David White, Jul 6, 2004
    #5
  6. const applies to the thing on its left. With the only exception
    of const beeing the leftmost keyword. Then it applies on the
    thing on its right.
     
    Karl Heinz Buchegger, Jul 6, 2004
    #6
  7. johny smith

    JKop Guest

    const int * chocolate;

    int const * chocolate;


    Both of the above are: const pointer to a non-const int. Note how the const
    is NOT touching the variable name, it's not directly beside it.


    int* const chocolate;

    Here, it's touching the variable name. What we have is: a non-const pointer
    to a const object.


    And then we have:

    const int* const chocolate;
    int const* const chocolate;

    a const pointer to a const object.


    -JKop
     
    JKop, Jul 6, 2004
    #7
  8. johny smith

    JKop Guest

    IGNORE MY LAST POST


    const int * chocolate;

    int const * chocolate;


    Both of the above are: non-const pointer to a const int. Note how the const
    is NOT touching the variable name, it's not directly beside it, so the
    varible itself is not const.


    int* const chocolate;

    Here, it's touching the variable name. What we have is: a const pointer
    to a non-const object.


    And then we have:

    const int* const chocolate;
    int const* const chocolate;

    a const pointer to a const object.


    -JKop
     
    JKop, Jul 6, 2004
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.