Is such a declaration legal?
Yes, although the effect isn't the same as if const precedes p's type.
Also, what is the difference between the following:
void f(const int *p)
Putting const before p's type protects the object that p points to.
Example:
void f(const int *p) {
int j;
*p = 0; //WRONG
p = &j; //legal
}
void f(int * const p)
Putting const after p's type protectsp itself.
Example:
void f(int * const p) {
int j;
*p = 0; //legal
p = &j; //WRONG
}
This feature isn't used very after since p is merely a copy of another
pointer, there's rarely any reason to protect it. An even greater rarity is the
next case.
void f(const int * const p)
Here const is protecting both p and the object it points to.
Example:
void f(const int * const p) {
int j;
*p = 0; //WRONG
p = &j; //WRONG
}
void f(int const *p)
This is the same as void f(const int *p).
See point 1.
void f(int const *p), which is the same asvoid f(const int *p).