#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Posts
    49
    Rep Power
    12

    What's the meaning of: int const *pt


    Is it equivalent to

    const int *pt

    or

    int *const pt

    :confused: What's the systematic way to determine the meaning of such expressions (with const qualifier)? Thanks a lot.
  2. #2
  3. No Profile Picture
    Overly white
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Location
    Fresno, CA
    Posts
    83
    Rep Power
    12
    Hey a C++ question that I can answer!!! I just read about doing this in my N00b book so I may still be wrong.

    Anyway, if I remeber correctly const int *pt can only point to a const integer, while int *const pt is a pointer that only points to one place and can not be changed. I think that is right ;).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    42
    Rep Power
    12
    Sounds good to me!:D
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,612
    Rep Power
    4247
    int const *p is equivalent to const int *p (at least according to my g++ 2.95 compiler). There is a difference between const int *p and int * const p though.

    const int *pi
    * Can be pointed at multiple variables.
    * Cannot be used to assign a new value to a variable via indirection.

    int *const ip
    * Can only be pointed to a single variable (done at initialization)
    * Can be used to assign a new value to a variable via indirection.

    const int * const pip
    (Yes, it is possible to declare a variable of this type too.)
    * Can only be pointed to a single variable (done at initialization)
    * Cannot be used to assign a new value via indirection

    To illustrate the differences:
    Code:
    #include <iostream>
    using namespace std;
    int main(void) {
      int x = 1;
      int y = 2;
    
      const int *pi;
      pi = &x;
      pi = &y; // Can be pointed to another variable
      *pi = 23; // <----- NOT ALLOWED
      cout << *pi << endl; // <--- Value can be accessed
    
      int * const ip = &x;
      *ip = 7; // <----- Can assign a value via indirection
      ip = &y; // <----- NOT ALLOWED
      cout << *ip << endl; // <---- Value can be accessed
    
      const int * const pip = &y;
      *pip = 42; // <------ NOT ALLOWED
      pip = &x; // <------ NOT ALLOWED
      cout << *pip << endl;
    
      return 0;
    }
    Hope this helps :)

IMN logo majestic logo threadwatch logo seochat tools logo