ProAnswers.org

What is the difference between const * char and const char *?

can you please explain me What is the difference between const * char and const char * ?

1 Like

they both are same.

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

In const char* p, the character pointed by ā€˜pā€™ is constant, so u cant change the value of character pointed by p but u can make ā€˜pā€™ refer to some other location. in char const* p,

the ptr ā€˜pā€™ is constant not the character referenced by it, so u cant make ā€˜pā€™ to reference to any other location but u can change the value of the char pointed by ā€˜pā€™.

I am sure you will get your answer along with example at below location

http://swsharinginfo.blogspot.com/2011/11/cdifference-between-const-char-p-and.html

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 ptr 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'; //possible as value at ptr is constant i.e. 'a'

p = &mychar2; //not possible as you cannot change address value of pointer