
May 22nd, 2003, 04:07 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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 
|