Thursday, February 17, 2011

const char *p and char * const p

Difference between const char *p and char * const p?

The former declares a pointer to a constant character; the latter declares a constant pointer to a character.

"char const *p" is a pointer to a constant character (you can't change the character);
"char * const p" is a constant pointer to a (variable) character (i.e. you can't change the pointer).

const char *p => cannot change the contents to which it is pointing
char *const p => cannot change the pointer address..

const char *p => cannot change the contents to which it is pointing
char *const p => cannot change the pointer address to which it is pointing

No comments:

Post a Comment