E
Elliot Marks
Jack said:The pointer p is initialized to point to the first character of the
unnamed array of characters holding the string literal.
In C++, the type of this array is array of constant char. In C the
language does not specify whether the string literal is constant or
not. But in both languages, it is undefined behavior is you try to
modify the string literal.
#include <stdio.h>
void func(char * p);
int main(void)
{
char * p = "abcdefg";
printf("%s\n", p);
func(p);
return 0;
}
void func(char * p)
{
*(p + 2) = 'z';
printf("%s\n", p);
}
This "seems" to work. The string literal is modified. Is this
undefined behaviour? If so, can I take advantage of it if the
code runs only on platform x with implementation y? That is, is
the behaviour consistent for a particular platform and
implementation, albeit undefined?