One does usually associate 'unmodifiable' with the term literal
char* str = "Hello World!";
*str = 'B'; // Bus Error!
However when using compound literals, I quickly discovered they are completely modifiable (and looking at the generated machine code, you see they are pushed on the stack):
char* str = (char[]){"Hello World"};
*str = 'B'; // A-Okay!
I'm compiling with clang-703.0.29
. Shouldn't those two examples generate the exact same machine code? Is a compound literal really a literal, if it's modifiable?
EDIT: An even shorter example would be:
"Hello World"[0] = 'B'; // Bus Error!
(char[]){"Hello World"}[0] = 'B'; // Okay!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…