Turbo C strcpy 库函数

发布于 2024-11-09 17:27:06 字数 350 浏览 4 评论 0原文

我发现 strcpy 函数只是将一个字符串复制到花药中。例如,如果程序包含以下语句:

char buffer[10];
----------
strcpy(buffer, "Dante");

字符串“Dante”将被放置在数组 buffer[] 中。该字符串将包含终止 null(\0),这意味着将复制总共六个字符。我只是想知道为什么我们不能通过更简单地说来实现相同的效果?

buffer = "Dante";

如果我没记错的话,C 对待字符串的方式比 BASIC 更像对待数组。

I discovered that the strcpy function simply copied one string to anther. For instance, if a program included the following statements:

char buffer[10];
----------
strcpy(buffer, "Dante");

the string "Dante" would be placed in the array buffer[]. The string would include the terminating null(\0), which means that six characters in all would be copied. I'm just wondering why we can't achieve the same effect more simply by saying?:

buffer = "Dante";

If I'm not mistaken, C treats strings far more like arrays than BASIC does.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

怪我闹别瞎闹 2024-11-16 17:27:06

因为字符串不是 C 中的数据类型。“字符串”是 char*,因此当您尝试分配它们时,您只是将内存地址而不是字符复制到缓冲区中。

考虑一下:

char* buffer;

buffer = malloc(20);
buffer = "Dante";

为什么它会神奇地将“Dante”放入缓冲区?

Because strings aren't a data type in C. "Strings" are char*s, so when you try to assign them, you are simply copying the memory address and not the characters into the buffer.

Consider this:

char* buffer;

buffer = malloc(20);
buffer = "Dante";

Why should it magically place "Dante" into the buffer?

巾帼英雄 2024-11-16 17:27:06

因为C中的“数组”是一块内存。没有要分配的指针。


如果您问为什么语法不是这样的:那么,如果长度不同会发生什么?

Because an "array" in C is a chunk of memory. There's no pointer to assign to.


If you're asking why the syntax isn't like that: Well, what would happen if the lengths were different?

甜味拾荒者 2024-11-16 17:27:06

数组地址不可更改。在其他意义上,您可以考虑,

char buffer[20];

编译时相当于,

char* const buffer = (char*)malloc(20);

现在,由于缓冲区地址无法更改,因此无法执行以下操作:

buffer = "Dante"; // error 'buffer' address is not modifiable

Address of array is not changeable. In other sense you can consider,

char buffer[20];

is a compile time equivalent of,

char* const buffer = (char*)malloc(20);

Now, since buffer address cannot be changed, one cannot perform operations like:

buffer = "Dante"; // error 'buffer' address is not modifiable
万劫不复 2024-11-16 17:27:06

你不能这样做 buffer = "Dante" 因为 C 中没有“字符串”数据类型,只有数组。

现在你可以做...

char buffer[10] = "Dante";

但是如果字符串的长度未知你可以做...

char buffer[] = "Dante123456678";

但只能在初始化期间,这意味着你不能做...

char buffer[];

buffer = "Dante";

you can't do buffer = "Dante" because there is no "string" data type in C, only arrays.

Now you CAN however do...

char buffer[10] = "Dante";

but if the length of the string is unknown you can do...

char buffer[] = "Dante123456678";

but only during initialization, meaning you can't do...

char buffer[];

buffer = "Dante";
岁月如刀 2024-11-16 17:27:06

当您编写buffer时,它被视为指向数组第一个元素的指针。当然,*bufferbuffer[0] 是第一个元素。由于 buffer 只是一个指针,因此您无法向其分配诸如 "Dante" 之类的一大堆数据。

When you write buffer, it is treated as a pointer to the first element of the array. Of course, *buffer or buffer[0] is the first element. Since buffer is just a pointer, you can't assign a whole bunch of data like "Dante" to it.

单挑你×的.吻 2024-11-16 17:27:06
  • 如果 char buffer[128]; 是声明,则 buffer 引用数组的第一个位置,因此 buffer = "Dante" 将尝试分配 字符串的地址到存储在数组中的地址。数组中的内存地址位置是只读的,并在编译时静态分配。因此,您不能执行 buffer = "Dante" 因为它试图更改指向编译时固定的其他位置的地址位置。这些位置无法写入。

  • 如果 char *buffer; 是声明,则 buffer 是指向 char 类型变量的指针,该变量可以指向星标块内存块。因此,当您将 buffer = "Dante" 时,字符串的地址放入 buffer 中。当您打印字符串时显示该字符串,因为它指向字符串起始地址,该地址被编译并存储在可执行文件中。但这不是首选方法。

  • 如果您执行char arr[] = "Dante";,则字符串“Dante”将存储在您可以写入的.text部分中,因此arr[0] = 'K' 像这样,即可以进行修改。

  • 如果您执行char *arr = "Dante";,则字符串“Dante”将存储在.rodata或类似的不可写位置。这是因为字符串文字按照标准是不可修改的。

  • If char buffer[128]; was the declaration, then buffer refers the first location of the array, so buffer = "Dante" will try assigning the address of the string onto the address which is stored in the array. Memory address location in the array are read only and statically assigned when compiled. So you cannot do buffer = "Dante" as it attempts to change an address location which points to some other location fixed at compile time. These locations cannot be written.

  • If char *buffer; was the declaration, then buffer is a pointer to a char type variable which can point to a starring chunk of memory blocks. So when you do buffer = "Dante" the address of the string into buffer. When will show the string when you print it, as it points to a string starting address, which is compiled in and stored in the executable. But this is not a preferred method.

  • If you do char arr[] = "Dante"; the string "Dante" gets stored in the .text section where you can write, so arr[0] = 'K' something like this , ie modification is possible.

  • If you do char *arr = "Dante"; then the string "Dante" gets stored in the .rodata or similar location which is not writeable. This is because string literals are not modifiable as per the standards.

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