In article <11************ **********@g14g 2000cwa.googleg roups.com>,
<bh******@dodo. com.au> wrote:
In the following code I am trying to change the contents of a string:
int main()
{
char *string="testin g";
The standard allows string literals to be stored in read-only memory.
An assignment of a string literal to a pointer sets the pointer value
to the address of that {possibly read-only} memory.
In particular, char *string="testin g"; usually does not allocate some
memory somewhere and copy the string into it at runtime. That's
one of the possible behaviours, but it isn't the only possible
behaviour.
Another thing to note is that it is allowed for the compiler
to merge all string literals -- so for example if you also had
char *anotherstring= "testing";
then anotherstring could end up as the same pointer value as
your string variable. Furthermore, if you had
char *thirdstring="j ust testing";
then string and anotherstring could end up pointing at the terminal
"testing" substring of the "just testing".
If you need a modifiable string, allocate the memory (somehow) and
copy the appropriate contents into it.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton