One way to remember the difference between char * const pointer and const char * pointer
char * const pointer;
means that the pointer is constant and immutable but the pointed data is not.const char * pointer;
means that the pointed data is constant and immutable but the pointer is not.An easy to think it is the const modifier is always bind to the next closest thing it can. So in
char * const pointer;
the const bind to the pointer variable directly, means the pointer itself can not be modified, but the value the pointer pointed to can be modified.
On the other hand, in this case
const char * pointer;
the const modifier bind to *pointer, this means the *pointer(which is the value the pointer pointed to) can not be modified while as the pointer itself can be modified.
评论
发表评论