Possible Duplicate:
const int = int const?
What is the difference between these combination of keywords ?
const int*
int const*
int* const
Is it possible to modify the value of a constant by accessing its address using a pointer ?
Possible Duplicate:
const int = int const?
What is the difference between these combination of keywords ?
const int*
int const*
int* const
Is it possible to modify the value of a constant by accessing its address using a pointer ?
const int*
int const*
Both declare an pointer to an constant type int
. It means that the pointer can be made to point to(hold) any other address but any attempt to modify the constant integer data type will result in Undefined Behavior.
int* const
Declares an constant pointer to an int
type. It means that the pointer cannot be assigned any new address it will always keep pointing to the same address, but the integer it points to can be changed.
Is it possible to modify the value of a constant by accessing its address using a pointer ?
Yes, it is possible but it leads to a ill formed program exhibiting an Undefined Behavior.
In general:
const T === T const
and
const T * === T const *
but
T * const
makes the pointer constant, but not necessarily the contents of T.
const T * const === T const * const
will make both the contents of T and the pointer itself constants.
As for changing the contents of a const
: you shouldn't be able to, but the compiler/OS/CPU don't try too hard to stop you, so you get what is colloquially called “undefined behaviour,” which is a nice way of saying “random crashes on some systems”
The first two are a variable pointer to a const int. The pointer itself can be changed. Without casting away const, the int can't be changed through this pointer. (It may, however, be modifiable through non-const references.)
The last is a const pointer to a variable int. Without casting away const, the pointer itself can't be changed. The int can be changed through the pointer.
const int*
and int const*
are both a pointer to a constant int. This means that the value pointed to can not be changed but the pointer itself can be.
The third example int *const
is a constant pointer to an int. This means that the value can be changed but the pointer can not.
Yes you can change the value of the address by using a pointer but doing so is undefined behavior and should be avoided. C++
also offers a const_cast
to remove the const
ness of a variable.
If you have any other questions related to the const
keyword you should reference the const correctness section of the C++ FAQ.
Const binds to the left.
Unless it is the left most clause then in binds right.
Just read right to left
const int* => (const int)* => "pointer" to "const int"
=> Pointer can change.
=> Can't change the value it points at (the int is const)
int const* => (int const)* => "pointer" to "const int"
=> As above
int* const => int (* const) => "const pointer" to "int"
=> Can't change the pointer (its const) it always points
=> at the same things.
=> You can change the value of the object it points at.