Difference between char* and const char*? Difference between char* and const char*? c c

Difference between char* and const char*?


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.


char *name

You can change the char to which name points, and also the char at which it points.

const char* name

You can change the char to which name points, but you cannot modify the char at which it points.
correction: You can change the pointer, but not the char to which name points to (https://msdn.microsoft.com/en-us/library/vstudio/whkd4k6a(v=vs.100).aspx, see "Examples"). In this case, the const specifier applies to char, not the asterisk.

According to the MSDN page and http://en.cppreference.com/w/cpp/language/declarations, the const before the * is part of the decl-specifier sequence, while the const after * is part of the declarator.
A declaration specifier sequence can be followed by multiple declarators, which is why const char * c1, c2 declares c1 as const char * and c2 as const char.

EDIT:

From the comments, your question seems to be asking about the difference between the two declarations when the pointer points to a string literal.

In that case, you should not modify the char to which name points, as it could result in Undefined Behavior.String literals may be allocated in read only memory regions (implementation defined) and an user program should not modify it in anyway. Any attempt to do so results in Undefined Behavior.

So the only difference in that case (of usage with string literals) is that the second declaration gives you a slight advantage. Compilers will usually give you a warning in case you attempt to modify the string literal in the second case.

Online Sample Example:

#include <string.h>int main(){    char *str1 = "string Literal";    const char *str2 = "string Literal";    char source[] = "Sample string";    strcpy(str1,source);    //No warning or error, just Undefined Behavior    strcpy(str2,source);    //Compiler issues a warning    return 0;}

Output:

cc1: warnings being treated as errors
prog.c: In function ‘main’:
prog.c:9: error: passing argument 1 of ‘strcpy’ discards qualifiers from pointer target type

Notice the compiler warns for the second case but not for the first.


char mystring[101] = "My sample string";const char * constcharp = mystring; // (1)char const * charconstp = mystring; // (2) the same as (1)char * const charpconst = mystring; // (3)constcharp++; // okcharconstp++; // okcharpconst++; // compile errorconstcharp[3] = '\0'; // compile errorcharconstp[3] = '\0'; // compile errorcharpconst[3] = '\0'; // ok// String literalschar * lcharp = "My string literal";const char * lconstcharp = "My string literal";lcharp[0] = 'X';      // Segmentation fault (crash) during run-timelconstcharp[0] = 'X'; // compile error// *not* a string literalconst char astr[101] = "My mutable string";astr[0] = 'X';          // compile error((char*)astr)[0] = 'X'; // ok