Create a modifiable string literal in C++
- by Anne
Is it possible to create a modifiable string literal in C++? For example:
char* foo[] = {
"foo",
"foo"
};
char* afoo = foo[0];
afoo[2] = 'g'; // access violation
This produces an access violation because the "foo"s are allocated in read only memory (.rdata section I believe). Is there any way to force the "foo"s into writable memory (.data section)? Even via a pragma would be acceptable! (Visual Studio compiler)
I know I can do strdup and a number of other things to get around the problem, but I want to know specifically if I can do as I have asked. :)