Difference between char* and const char*?

Iceman picture Iceman · Mar 23, 2012 · Viewed 328k times · Source

What's the difference between

char* name

which points to a constant string literal, and

const char* name

Answer

ankit.karwasra picture ankit.karwasra · Mar 23, 2012

char* is a mutable pointer to a mutable character/string.

const char* is a mutable pointer to an immutable character/string. You cannot change the contents of the location(s) this pointer points to. Also, compilers are required to give error messages when you try to do so. For the same reason, conversion from const char * to char* is deprecated.

char* const is an immutable pointer (it cannot point to any other location) but the contents of location at which it points are mutable.

const char* const is an immutable pointer to an immutable character/string.