0

For a given type T:

What is the difference between const T * and T * const?

Also, are there other places that const can go? For instance, is T const * a thing? Can you have more than one const in an expression, such as const T * const?

Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
  • 3
    Use the [spiral rule](http://c-faq.com/decl/spiral.anderson.html) and it should be a lot clearer what the difference is and that it can go "before" anything it can apply to. – chris Aug 13 '14 at 18:32

1 Answers1

7

const T *x means x points to an object of type T, but that object should not be modified.

On the other hand, T *const x means the pointer itself cannot be modified (but the object it points to can).

const T *const x is just a combination of the two; both the pointer and the object it points to cannot be modified.

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57