0

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 ?

Community
  • 1
  • 1
Diode
  • 24,570
  • 8
  • 40
  • 51
  • Thinking in C++ by Bruce Eckel is an excellent book that explains most all the corners of C++ including a section about the differences of syntax similar to this when using const and * –  Dec 21 '11 at 20:16
  • const binds to the left unless it is on the very left then in binds right. – Martin York Dec 21 '11 at 20:45

6 Answers6

3
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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    Correction - the int is not necessarily a constant. Attempts to modify the int through the pointer will result in an error at *compile-time.* – Andy Thomas Dec 21 '11 at 20:13
2

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”

BRPocock
  • 13,638
  • 3
  • 31
  • 50
1

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.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
1

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 constness 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.

Joe
  • 56,979
  • 9
  • 128
  • 135
0

This may be helpful. This is another one that talks about breaking it apart so you can see which parts become const in various scenarios.

OnlineCop
  • 4,019
  • 23
  • 35
0

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.
Martin York
  • 257,169
  • 86
  • 333
  • 562