Background:
We are talking about modifying string literals. For example:
int main()
{
char * p = "Hello";
*p = 'Y';
}
ce*******@germanosnet.gr (Lefteris Laskaridis) wrote in message news:<a9**************************@posting.google. com>...
"Ron Natalie" <ro*@sensor.com> wrote in message news:<40**********************@news.newshosting.co m>...
Purely by coincidence. One of those subtle problems with undefined
behavior
it runed correctly in bcc55, visual c++ 6 and borland turbo c++ 3.0 on IA-32.
By "correctly," you must mean "as expected"; because there isn't *one*
correct way of running when undefined behavior is involved. For
example, the program above aborts with a 'Segmentation fault' when
compiled with gcc 3.2 and run on Linux. (Using gcc 2.95 doesn't make
any difference.)
String literals are actually arrays of constant characters. The C++
standard allows non-const pointers to be initialized with string
literals to avoid breaking old C code. Attempting to modify the string
literal through such a pointer is undefined behavior.
So, actually
char const * p = "Hello";
would be the correct definition of a pointer pointing to the first
constant character of the string literal.
Ali