在 char 指针上使用 strcpy 时程序中止? (在 char 数组上工作正常)

发布于 2024-10-31 15:52:40 字数 218 浏览 4 评论 0原文

我很困惑为什么以下不起作用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

虐人心 2024-11-07 15:52:40

"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.

っ左 2024-11-07 15:52:40

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 place

char s[] = "ddd"; defines a char array on the stack which is writable.

眼睛会笑 2024-11-07 15:52:40

在第一个示例中,您有一个指向字符串文字的指针。该指针实际上应该是 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 a char * 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.

一抹苦笑 2024-11-07 15:52:40

第一个示例是 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 be const char *f = "abcdef";, which strcpy won't take.

太傻旳人生 2024-11-07 15:52:40

语句 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 a const char.
All you're doing is creating a pointer in memory and then writing over the next 6 bytes, which is illegal in C.

天赋异禀 2024-11-07 15:52:40

大多数编译器将字符串文字视为只读,因此它们所在的内存可以标记为只读,从而导致运行时错误。

要使其工作,请执行以下操作:

char * f = strdup("abcdef");
strcpy(f, "abcdef");
printf("%s",f);
free(f);

这会在堆内存中创建字符串的可修改副本,当然,该副本需要在程序结束时释放。

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:

char * f = strdup("abcdef");
strcpy(f, "abcdef");
printf("%s",f);
free(f);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文