HarishTheLearner wrote:
-> Can we modify the const char* like shown above after initialization?!
Yes, (like Obama) we can.
HarishTheLearner wrote:
does the c_str() function return actual base address of string's character array?If yes then why is the s1 value not changed?
Again, yes (see
basic_string::c_str[
^] documentation).
A little explanation:
const char* s2 = s1.c_str();
Declares
s2
as a pointer (a variable containing an address) to a (const) sequence of characters.
The same statement initializes it to
point at
s1
character sequence.
s2 = "Two\n";
This change to
s2
value modifies just the
pointed address: now
s2
points to the character sequence (the literal string)
"Two\n"
, so that it points
no more to
s1
character sequence.
Now the output should be obvious.
BTW:
never try to change the
std::string
content using the address provided by
c_str()
method.
:-)