2

Possible Duplicate:
what is the difference between const int*, const int * const, int const *

Can someone explain this type to me?

char const * const blah;

I would have expected it to be written something like: const char *. what does the const post the type mean? e.g. int const foo.

and then, what does the first statement mean?

Community
  • 1
  • 1
Dollarslice
  • 9,917
  • 22
  • 59
  • 87

2 Answers2

4

Constant pointer to constant char. You can't change the address it points to and you can't change the character(s) at the end of it.

BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • I'm confused as to why it isn't const int * const – Dollarslice Feb 28 '12 at 17:22
  • @SirYakalot : `const int foo` is identical to `int const foo`. By similar logic, `const int * const foo` is identical to `int const * const foo`. – Robᵩ Feb 28 '12 at 17:25
  • I never new you could put the const before OR after. huh. what about const int const *? – Dollarslice Feb 28 '12 at 17:29
  • @SirYakalot: That's an error - you can only specify `const` once. You could write `const int volatile` if you really wanted to. – Mike Seymour Feb 28 '12 at 17:32
  • You could even argue that it's best in general to always put the const *after* the type, because you read complicated types from the inside out, and because in this particular instance, the const relating the the pointer *has* to go after the *. However, nobody actually bothers with this. – BoBTFish Feb 28 '12 at 17:36
2
char const * const ptr = "Hello";

ptr is a const pointer to a constant character(s). Which means neither ptr nor the data it is pointing a can be modified. Since pointer it self is constant type, it needs to be intialized because it later can not be reassigned.

char const * const laterPtr; // Wrong.
Mahesh
  • 34,573
  • 20
  • 89
  • 115