const char *p - This is a pointer to a constant char. One cannot change the value
pointed at by p, but can change the pointer p itself.
*p = 'A' is illegal.
p = "Hello" is legal.
Note that even char const *p is the same!
const * char p - This is a constant pointer to (non-const) char. One cannot change
the pointer p, but can change the value pointed at by p.
*p = 'A' is legal.
p = "Hello" is illegal.
const char * const p - This is a constant pointer to constant char! One cannot
change the value pointed to by p nor the pointer.
*p = 'A' is illegal.
p = "Hello" is also illegal.
So when const is nearest to the type, or near to both type and pointer operator( *), it is type which is const. When const is near to the *, it is pointer which is constant. So, const has more affinity for type(like char etc).
i.e.CT - const is Type
Both the forms are equivalent. Keywords in the brackets are optional. The simplest tip here is to notice the relative position of the `const' keyword with respect to the asterisk (*).
Saturday, December 5, 2009
What's the difference between const char *p, char * const p and const char * const p?
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment