简体   繁体   English

Turbo C strcpy 库 function

[英]Turbo C strcpy library function

I discovered that the strcpy function simply copied one string to anther.我发现 strcpy function 只是将一个字符串复制到花药。 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[].字符串“Dante”将被放置在数组 buffer[] 中。 The string would include the terminating null( \0 ), which means that six characters in all would be copied.该字符串将包含终止的 null ( \0 ),这意味着将复制总共六个字符。 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.如果我没记错的话,C 对待字符串的方式比 BASIC 更像 arrays。

Because strings aren't a data type in C.因为字符串不是 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. “字符串”是char* ,因此当您尝试分配它们时,您只是将 memory 地址而不是字符复制到缓冲区中。

Consider this:考虑一下:

char* buffer;

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

Why should it magically place "Dante" into the buffer?为什么要神奇地将“Dante”放入缓冲区?

Because an "array" in C is a chunk of memory.因为 C 中的“数组”是 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,其他意义上,你可以考虑,

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地址无法更改,因此无法执行以下操作:

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

When you write buffer , it is treated as a pointer to the first element of the array.当您写入buffer时,它被视为指向数组第一个元素的指针。 Of course, *buffer or buffer[0] is the first element.当然, *bufferbuffer[0]是第一个元素。 Since buffer is just a pointer, you can't assign a whole bunch of data like "Dante" to it.由于buffer只是一个指针,因此您不能将一大堆数据(如"Dante"分配给它。

you can't do buffer = "Dante" because there is no "string" data type in C, only arrays.您不能执行buffer = "Dante" ,因为 C 中没有“字符串”数据类型,只有 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";
  • If char buffer[128];如果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.是声明,然后buffer引用数组的第一个位置,因此 buffer = "Dante" 将尝试将字符串的地址分配给存储在数组中的地址。 Memory address location in the array are read only and statically assigned when compiled. Memory 数组中的地址位置是只读的,编译时静态分配。 So you cannot do buffer = "Dante" as it attempts to change an address location which points to some other location fixed at compile time.所以你不能做buffer = "Dante"因为它试图改变一个地址位置,它指向在编译时固定的某个其他位置。 These locations cannot be written.无法写入这些位置。

  • If char *buffer;如果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.是声明,然后buffer是指向char类型变量的指针,该变量可以指向 memory 块的主演块。 So when you do buffer = "Dante" the address of the string into buffer .因此,当您执行buffer = "Dante",将字符串的地址转换为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";如果你这样做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.字符串“Dante”存储在您可以编写的.text部分中,因此arr[0] = 'K'是这样的,即可以进行修改。

  • If you do char *arr = "Dante";如果你这样做char *arr = "Dante"; then the string "Dante" gets stored in the .rodata or similar location which is not writeable.然后字符串“Dante”被存储在不可写的.rodata或类似位置。 This is because string literals are not modifiable as per the standards.这是因为字符串文字不能按照标准进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM