What is difference between char* and char const*

In c++ I am trying to convert std::string to c style string.
I found a following function

 
string.c_str(); 


Its return type is char const* not simply char *
I am just curious to know what is the difference between both.


1
2
3
4
5
6

    string a;
    a="abc";
    char const* ptr = a.c_str();
    cout<<*ptr;
char * and const char * are two pointers. The difference is that const char * points to a constant object that is you may not change the object through the pointer of type const char *.

For example

char c = 'A';

char *cp = &c;
const char *ccp = &c;

*cp = 'B'; // O'k, c will be equal to 'B'
*ccp = 'C'; // error! you may not change object c through const char *
Last edited on
Topic archived. No new replies allowed.