In C99, I believe that modifying string literals is undefined behavior. I do not have a copy of this standard, but I have a C1X project (n1570), which states in clause 6.4.5 of clause 7:
It is not known whether these arrays are different if their elements have corresponding values. If the program tries to change such an array, the behavior is undefined.
I found the question stack overflow that addresses this topic and contains the following comment by Jonathan Leffler:
Initially, the C89 (C90) standard did not prohibit changing literals because there was too much code written before the standard that would violate it.
But I also discussed a lot of the type of string literals and the fact that they char[N]are not const char[N]. I understand that this decision was made so that most of the existing code is not violated.
Can someone give me a definitive answer. Is the string literary modification of UB in C89?
source
share