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. Advertising

  2. johny smith

    John Carson Guest

    "johny smith" <> wrote in message
    news:
    > 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.


    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.

    > 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++;
    > }



    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
     
    John Carson, Jul 6, 2004
    #2
    1. Advertising

  3. johny smith

    David White Guest

    "johny smith" <> wrote in message
    news:...
    > 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 );


    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.

    >
    >
    > int main()
    > {
    >
    > int a = 5;
    >
    > f( &a );
    >
    >
    >
    > return 0;
    >
    > }
    >
    >
    > void f( int const * b )
    > {
    >
    > b++;
    > }


    DW
     
    David White, Jul 6, 2004
    #3
  4. On Mon, 5 Jul 2004 19:58:38 -0600, johny smith
    <> wrote:

    > 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
    >


    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

    "John Harrison" <> wrote in message
    news:eek:psao9dtlz212331@andronicus...
    > On Mon, 5 Jul 2004 19:58:38 -0600, johny smith
    > <> wrote:
    >
    > Others have explained your mistake, but maybe your confusion arises from
    > ambiguous terminolgy. Constant pointers are pretty rare


    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.

    > (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.


    DW
     
    David White, Jul 6, 2004
    #5
  6. Re: don't understand this deal with const pointers in this trivialexample

    johny smith wrote:
    >
    > 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?


    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
     
    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. 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. Replies:
    3
    Views:
    436
  2. rbt

    deal or no deal

    rbt, Dec 22, 2005, in forum: Python
    Replies:
    7
    Views:
    561
    Duncan Smith
    Dec 28, 2005
  3. baibaichen

    trivial or non-trivial object

    baibaichen, Jan 12, 2006, in forum: C++
    Replies:
    3
    Views:
    912
    osmium
    Jan 12, 2006
  4. =?gb2312?B?wNbA1rTzzOzKpg==?=
    Replies:
    26
    Views:
    6,550
    Ian Collins
    Jan 30, 2007
  5. Javier
    Replies:
    2
    Views:
    574
    James Kanze
    Sep 4, 2007
Loading...

Share This Page