Search code examples
cpointersconstants

Difference between char* and const char*?


What's the difference between

char* name

which points to a constant string literal, and

const char* name

Solution

  • 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.