C 中的字符串常量与字符数组
更多的是一个一般性问题,而不是试图修复某些问题,我一直在阅读 C 编程语言书籍,他们很注意区分两者之间的区别,
char amessage[] = "blah";
char *pmessage = "blah";
其中一个是 char数组和另一个指向字符串常量的指针。他们说修改 char 数组是可以接受的,但你不应该修改字符串常量,因为它会触发未定义的行为。我的问题是:字符串常量存储在内存中的方式不是与字符数组相同吗?为什么我可以修改它,就像
char *p = "this is a string constant";
*(p+2) = 'a';
printf("%s", p);
您所期望的那样打印“这是一个字符串常量”。我可以理解它是如何有意义的,因为字符串 constant 不应该在运行时最终被更改,因为它可能会让其他人/你自己在代码上工作时感到困惑,不希望它的值发生变化,但纯粹是功能术语它有什么问题,可能触发什么未定义的行为以及当 char 数组不会触发时它如何机械地适得其反?我只是想知道我是否遗漏了有关字符串常量如何在内存中工作以及编译器如何看待它们的信息。
Possible Duplicate:
What is the difference between char s[] and char *s in C?
More of a general question rather than trying to fix something, I've been reading the C programming language book and they take care to make the distinction between
char amessage[] = "blah";
char *pmessage = "blah";
The difference being one is a char array and the other a pointer to a string constant. They say modifying the char array is acceptable but you shouldn't modify string constants as it triggers undefined behavior. My question is: isn't the string constant stored in memory the same way the char array is? Why can I modify it as in
char *p = "this is a string constant";
*(p+2) = 'a';
printf("%s", p);
Ends up printing "thas is a string constant" as you might expect. I can understand how it would make sense as a string constant shouldn't end up being changed at run time, as it might confuse others/yourself working on your code not expecting it's value to change but in purely functional terms what is wrong with it, what is the undefined behavior that might trigger and how mechanically could it backfire when a char array wouldn't? I'm just wondering if I'm missing something about how string constants work in memory and how they are seen by the compiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
至少在我的计算机上,以下程序崩溃了:
如果它看起来对您有用(可能在某些嵌入式编译器上),那么您只是很幸运。未定义的行为意味着程序可以做任何事情。请参阅 http://blog.regehr.org/archives/213 。
另请参阅 char 和 char 之间有什么区别s[] 和 char *s?。
At least on my computer, the following program crashes:
If it appears to be working for you (which it might on certain embedded compilers), you're just getting lucky. Undefined behavior means the program is allowed to do anything. See http://blog.regehr.org/archives/213 .
See also What is the difference between char s[] and char *s?.
如果是 char 数组,字符串文字
"blah"
的内容也会复制到堆栈上。所以你可以在不调用 UB 的情况下修改它,因为毕竟它只是一个副本。对于 char * ,您实际上会尝试根据 UB 标准修改原始字符串文字。
In case of char array the content of the string literal
"blah"
are copied on to the stack as well. So you can modify it without invoking UB because after all its just a copy.In case of
char *
you actually try to modify the original string literal and as per the Standard that is UB.对于字符串常量,无法保证数据将存储在哪里——编译器可以自由地执行它想要的任何操作,因为应该禁止您写入它。例如,指针实际指向加载的可执行代码本身中定义字符串常量的位置并不是闻所未闻的。
With a string constant, there's not guarantee about where the data will be stored -- the compiler is free to do any tricks it wants, because you're supposed to be forbidden from writing to it. So for example, it's not unheard of for the pointer to actually point to the spot in the loaded executable code itself where the string constant is defined.