Difference between const char *p and char *const p

const char *p and char *const p  both look similar, but are different!
const char *p -- It is a non constant pointer to constant data. That means the data to which it is pointing can never be changed.
For example,
char mychar = 'a';
const char *p = & mychar;
*p = 'b'; //not possible as value at pointer is constant i.e. 'a'

char *const p  -- It is a constant pointer to non constant data. That means, this pointer points to address that is constant and thus pointer cannot point to other address.
For example,
char mychar = 'a';
char mychar2 = 'z';
const char *p = &mychar;
*p = 'b';
p = &mychar2; //not possible as you cannot change address value of pointer


Post a Comment