S
S S
Hi Everyone
I have
const char *p = "Hello";
So, here memory is not allocated by C++ compiler for p and hence I
cannot access p[0] to modify the contents to "Kello"
p[0] = 'K'; // error at runtime
So I did
const char *p = new char[6];
But then how do I initialize it to "Hello"? My requirement is - I want
a const char* initialised and later want to modify the contents.
I know a way as written below
const char p[] = "hello";
const_cast<char&>(p[0]) = 'K'; //OK
But how to acheive this with pointers?
What I know is - C compiler allocates memory when I do
const char* = "hello";
But C++ compiler does not do that. Any help is welcome.
Thanks
SS
I have
const char *p = "Hello";
So, here memory is not allocated by C++ compiler for p and hence I
cannot access p[0] to modify the contents to "Kello"
p[0] = 'K'; // error at runtime
So I did
const char *p = new char[6];
But then how do I initialize it to "Hello"? My requirement is - I want
a const char* initialised and later want to modify the contents.
I know a way as written below
const char p[] = "hello";
const_cast<char&>(p[0]) = 'K'; //OK
But how to acheive this with pointers?
What I know is - C compiler allocates memory when I do
const char* = "hello";
But C++ compiler does not do that. Any help is welcome.
Thanks
SS