int* const ???

This is a discussion on int* const ??? within the C++ Programming forums, part of the General Programming Boards category; I've just seen a function like show below, can someone explain why the const is after the int*? Code: void ...

  1. #1
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    int* const ???

    I've just seen a function like show below, can someone explain why the const is after the int*?

    Code:
    void swap( int* const ptr1, int* const ptr2 )
    {
       int temp;
       temp = *ptr1;
       *ptr1 = *ptr2;
       *ptr2 = *temp;
    }
    Last edited by endo; 08-03-2002 at 04:11 AM.
    Couldn't think of anything interesting, cool or funny - sorry.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    It tells you that ptr1 and ptr2 are constants (within this function at least)

    So it stops you from doing (by mistake) something like
    ptr1 = &temp;

    If you try it, the compiler will complain about it.

  3. #3
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    So its the same as:

    void swap( const int* ptr1, const int* ptr2 )

    I've just never seen that format anywhere before.
    Couldn't think of anything interesting, cool or funny - sorry.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    With thanks to Scott Meyers (Effective C++) :
    Code:
    char *p              = "Hello";  // non-const pointer,
                                     // non-const data
                                     // note: deprecated from C++ Standard
                                     // const char [] is standard for string literal types
    
    const char *p        = "Hello";  // non-const pointer,
                                     // const data
    
    char * const p       = "Hello";  // const pointer,
                                     // non-const data
    
    const char * const p = "Hello";  // const pointer,
                                     // const data
    Meyers continues his explanation by suggesting that an imaginary vertical line be drawn through the asterisk and if

    a. 'const' is to the left of the line, what's pointed to is const;

    b. 'const' is to the right of the line, the pointer is const, and;

    c. 'const' is on both sides of the line, both on const.

    No doubt more than you were interested in asking about, but pointers - at least for those with minimal, or no, background in C - do seem troublesome. I also found the "vertical line" imagery helpful with regard to 'const'.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > So its the same as:
    > void swap( const int* ptr1, const int* ptr2 )
    No, this is the exact opposite

    ptr1 = &temp; // is valid

    *ptr1 = temp; // is invalid

    See skipper's post

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Thanks Skipper, Salem - thats exactly what I was looking for. I can sleep at night again now
    Couldn't think of anything interesting, cool or funny - sorry.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    A good way to keep this straight in your head is to read the statement backwards...

    int const x; // x is a constant int

    int const *x; // x is a pointer to a constant int

    int *const x; // x is a constant pointer to an int

    int const *const x; // x is a constant pointer to a constant int

    In the first statement - once initialized - x cannot be reassigned a new value.

    In the second statement x can be reassigned to a new memory address, but only an address containing a const int type.

    In the third statement - once initialized - x cannot be reassigned to a new memory address, but the int type value at that address can be reassigned a new value.

    In the fourth statement both the address and the value at that address cannot be reassigned after initialization, they are both constant
    Last edited by DarkStar; 08-03-2002 at 10:50 AM.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    DarkStar,

    Good information and a nice twist on what's been presented.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  9. #9
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Why does this compile without problems though?

    CString const* getSQL( );

    What effect would this have?
    Couldn't think of anything interesting, cool or funny - sorry.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > CString const* getSQL( );
    The function returns a pointer to a const CString

    It basically means you wont be able to modify the CString via this pointer (you can look, but you can't touch)

  11. #11
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    same as
    Code:
    const CString* getSQL( )
    then?
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21