Turbo C strcpy 库函数
我发现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
因为字符串不是 C 中的数据类型。“字符串”是 char*,因此当您尝试分配它们时,您只是将内存地址而不是字符复制到缓冲区中。
考虑一下:
为什么它会神奇地将“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:
Why should it magically place "Dante" into the buffer?
因为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?
数组地址不可更改。在其他意义上,您可以考虑,
编译时相当于,
现在,由于
缓冲区
地址无法更改,因此无法执行以下操作:Address of array is not changeable. In other sense you can consider,
is a compile time equivalent of,
Now, since
buffer
address cannot be changed, one cannot perform operations like:你不能这样做
buffer = "Dante"
因为 C 中没有“字符串”数据类型,只有数组。现在你可以做...
但是如果字符串的长度未知你可以做...
但只能在初始化期间,这意味着你不能做...
you can't do
buffer = "Dante"
because there is no "string" data type in C, only arrays.Now you CAN however do...
but if the length of the string is unknown you can do...
but only during initialization, meaning you can't do...
当您编写
buffer
时,它被视为指向数组第一个元素的指针。当然,*buffer
或buffer[0]
是第一个元素。由于buffer
只是一个指针,因此您无法向其分配诸如"Dante"
之类的一大堆数据。When you write
buffer
, it is treated as a pointer to the first element of the array. Of course,*buffer
orbuffer[0]
is the first element. Sincebuffer
is just a pointer, you can't assign a whole bunch of data like"Dante"
to it.如果
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, thenbuffer
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 dobuffer = "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, thenbuffer
is a pointer to achar
type variable which can point to a starring chunk of memory blocks. So when you dobuffer = "Dante"
the address of the string intobuffer
. 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, soarr[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.