While the problem is already solved by the answers above, I do miss the reason why...
So maybe as a rule of thumb:
- The
const
always refers to it's predecessor token.
- In case there is no such, it's "consting" it's successor token instead.
This rule can really help you out for declaring a pointer to const pointers or something equally neat.
Anyway, with this in mind, it should get clear why
struct Person *const person = NULL;
declares a const pointer to a mutable struct.
Think about it, your typedef "groups" the struct Person
with the pointer token *
.
So, for writing
const PersonRef person = NULL;
your compiler sees something like this (pseudo-code):
const [struct Person *]person = NULL;
As there's nothing to const
's left, it deklares the token to it's right struct Person *
constant.
Well I think, this is the reason why I don't like hiding pointers by typedefs, while I do like typedefs as such. What about writing
typedef struct Person { ... } Person;
const Person *person; /*< const person */
Person *const pointer; /*< const pointer to mutable person */
and it should be quite clear to compilers and humans, what you're up to.