> int value = 15;
You've requested this memory for your use. String literals are not meant to be changed ever because they are stored in memory that is not meant to be overwritten, merely accessed. This is to protect you from wantonly changing the return values of some functions.
This chrashes as it should.
Code:
#include <iostream>
using namespace std;
char *f()
{
return "hello";
}
int main()
{
char *ptr = f();
*ptr = 'j';
cout << ptr << "\n";
return 0;
}
The sane way is to allocate memory for your string literal so that you can change it.
Code:
#include <iostream>
#include <cstring>
using namespace std;
const char *f()
{
return "hello";
}
int main()
{
char arr[6];
strcpy(arr, f());
*arr = 'j';
cout << arr << "\n";
return 0;
}