string literal question

Hi all,
I am reading about string and find the following in the book:
Some compilers treat string literals as read-only constants, leading to a runtime error if you try to write new data over them. That string literals be constant is the mandated behavior in C++, but not all compilers have made that change from older behavior yet.

Does it mean if a char pointer pointing to a string literal, it is illegal to change the value as follows?
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main ()
{
        char animal[20] = "bear";
        animal[0] = 'f';
        return 0 ;
}



Hmmm ... The above one works, so how do we define "string literal" vs. a bunch of chars ?
That would be correct, except declaring a character array like you have actually makes a static array populated with the C-string you have given. Change the array to a char* and you would be doing something illegal.
Thanks.
Topic archived. No new replies allowed.