Question about declaring const pointer to const value

Discussion in 'C Programming' started by linq936, Sep 18, 2007.

  1. linq936

    linq936 Guest

    Hi,
    I have the following code:

    int main(){ /* line 1 */
    const int i = 10; /* line 2 */
    const int j = 20; /* line 3 */
    /* line 4 */
    int const * const p = &i; /* line 5 */
    const int * const p2 = &j; /* line 6 */
    /* line 7 */
    (*p2)++; /* line 8 */
    (*p)++; /* line 9 */

    return 0;
    }

    Compiler does not complain line 6 and errors out on those
    incremental statements of line 8 and 9.

    So the declaration on line 5 and line 6 are the same? I mean "int
    const * const" and "const int * const" are same?

    It is a little hard to grasp how to use "const" in declaration.

    Any tip?
     
    linq936, Sep 18, 2007
    #1
    1. Advertising

  2. linq936

    CBFalconer Guest

    linq936 wrote:
    >
    > I have the following code:
    >
    > int main(){ /* line 1 */
    > const int i = 10; /* line 2 */
    > const int j = 20; /* line 3 */
    > /* line 4 */
    > int const * const p = &i; /* line 5 */
    > const int * const p2 = &j; /* line 6 */
    > /* line 7 */
    > (*p2)++; /* line 8 */
    > (*p)++; /* line 9 */
    >
    > return 0;
    > }
    >
    > Compiler does not complain line 6 and errors out on those
    > incremental statements of line 8 and 9.
    >
    > So the declaration on line 5 and line 6 are the same? I mean "int
    > const * const" and "const int * const" are same?
    >
    > It is a little hard to grasp how to use "const" in declaration.


    The thing that counts is which side of the * the 'const' is.
    "const int *p" means p is a non-constant pointer which points at
    read-only int(s). "int * const p = &x;" means p is a read-only
    pointer, set to point at the variable x, and which can only be set
    at this declaration point.

    Try to use fixed width fonts, so as to avoid the silly widths.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 18, 2007
    #2
    1. Advertising

  3. linq936

    Richard Bos Guest

    linq936 <> wrote:

    > So the declaration on line 5 and line 6 are the same? I mean "int
    > const * const" and "const int * const" are same?
    >
    > It is a little hard to grasp how to use "const" in declaration.
    >
    > Any tip?


    Yes. Read the friggin' FAQ already.

    <http://c-faq.com/ansi/constptrconst.html>

    Richard
     
    Richard Bos, Sep 18, 2007
    #3
    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:
    10
    Views:
    745
    Chris Torek
    Feb 4, 2005
  2. coala
    Replies:
    3
    Views:
    419
    coala
    Sep 6, 2006
  3. coala
    Replies:
    1
    Views:
    616
    Victor Bazarov
    Sep 6, 2006
  4. Javier
    Replies:
    2
    Views:
    627
    James Kanze
    Sep 4, 2007
  5. Disc Magnet
    Replies:
    1
    Views:
    655
    Ian Collins
    May 6, 2010
Loading...

Share This Page