在 char 指针上使用 strcpy 时程序中止? (在 char 数组上工作正常)
我很困惑为什么以下不起作用:
char * f = "abcdef";
strcpy(f, "abcdef");
printf("%s",f);
char s[] = "ddd";
strcpy(&s[0], "eee");
printf("%s", s);
在两个例子中 strcpy 都收到了一个 char * 但在第一个例子中它死得很惨。
I'm perplexed as to why the following doesn't work:
char * f = "abcdef";
strcpy(f, "abcdef");
printf("%s",f);
char s[] = "ddd";
strcpy(&s[0], "eee");
printf("%s", s);
In both examples strcpy received a char * yet on the first example it dies a horrible death.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
"abcdef"
和"ddd"
是字符串文字,可能驻留在地址空间的只读部分。char s[] = "ddd"
确保此文字被复制到堆栈 - 因此它是可修改的。"abcdef"
and"ddd"
are string literals which may reside in a read-only section of your address space.char s[] = "ddd"
ensures this literal is copied to stack - so it's modifiable.char * f = "abcdef";
定义了一个指向“abcdef”的 char 指针,该指针位于只读区域,因此您无法写入该位置char s[] = "ddd ";
定义了一个在堆栈上可写的字符数组。char * f = "abcdef";
defines a char pointer to "abcdef" which is located in read-only area so you can't write to this placechar s[] = "ddd";
defines a char array on the stack which is writable.在第一个示例中,您有一个指向字符串文字的指针。该指针实际上应该是 const char *,因为任何修改字符串文字的尝试都是未定义的行为。但是,由于遗留原因,您可以使用
char *
来指向它。但您仍然不应该尝试修改它。在第二个版本中,您有一个沼泽标准数组,其内容恰好被初始化为相当于您的字符串。这是可以修改的,因为它是您的数组。
In the first example, you have a pointer to a string literal. This pointer should really be
const char *
, because any attempt to modify a string literal is undefined behaviour. However, for legacy reasons allows you to use achar *
to point at it. But you still shouldn't be trying to modify it.In the second version, you have a bog-standard array, whose contents happen to be initialised to be equivalent to your string. This is modifiable, as it's your array.
第一个示例是
char *
到字符文字(文字是"something"
)。字符文字是只读的,尝试写入它们可能会导致崩溃。您的第一个指针实际上应该是const char *f = "abcdef";
,strcpy
不会采用它。The first example is a
char *
to a character literal (a literal is"something"
). Character literals are read-only, and attempting to write to them can result in crashes. Your first pointer should really beconst char *f = "abcdef";
, whichstrcpy
won't take.语句 char * f = "abcdef" 将内存中的一个点分配给文字字符串“abcdef”,但是在动态分配内存之前,它将拒绝让您修改其内容 - 它相当于
const char
。您所做的就是在内存中创建一个指针,然后写入接下来的 6 个字节,这在 C 中是非法的。
The statement
char * f = "abcdef"
assigns a point in memory to the literal string "abcdef", however it will refuse to let you modify its contents until the memory is dynamically allocated - it's equivalent to aconst char
.All you're doing is creating a pointer in memory and then writing over the next 6 bytes, which is illegal in C.
大多数编译器将字符串文字视为只读,因此它们所在的内存可以标记为只读,从而导致运行时错误。
要使其工作,请执行以下操作:
这会在堆内存中创建字符串的可修改副本,当然,该副本需要在程序结束时释放。
String literals are considered readonly by most compilers, so the memory where they reside can be marked as readonly, resulting in a runtime error.
To make it work, do the following:
This creates a modifiable copy of the string in the heap memory, which needs to get freed at the end of your program of course.