简体   繁体   English

strcpy() 的属性

[英]Properties of strcpy()

I have a global definition as following:我有一个全局定义如下:

#define globalstring "example1"

typedef struct
{   
    char key[100];
    char trail[10][100];
    bson_value_t value;

} ObjectInfo;

typedef struct
{
    ObjectInfo CurrentOrderInfoSet[5];

} DataPackage;

DataPackage GlobalDataPackage[10];

And I would like to use the strcpy() function in some of my functions as following:我想在我的一些函数中使用 strcpy() 函数,如下所示:

strcpy(GlobalDataPackage[2].CurrentOrderInfoSet[0].key, "example2");

char string[100] = "example3";
strcpy(GlobalDataPackage[2].CurrentOrderInfoSet[0].key, string);

strcpy(GlobalDataPackage[2].CurrentOrderInfoSet[0].key, globalstring);

First question: Are the global defined strings all initiated with 100 times '\\0'?第一个问题:全局定义的字符串是否都以100次'\\0'开头?

Second qestion: I am a bit confused as to how exactly strcpy() works.第二个问题:我对 strcpy() 的工作原理有点困惑。 Does it only overwrite the characters necessary to place the source string into the destination string plus a \\0 at the end and leave the rest as it is or does it fully delete any content of the destination string prior to that?它是否只覆盖将源字符串放入目标字符串所需的字符加上末尾的 \\0 并将其余部分保持原样,还是在此之前完全删除目标字符串的任何内容?

Third question: All my strings are fixed length of 100. If I use the 3 examples of strcpy() above, with my strings not exceeding 99 characters, does strcpy() properly overwrite the destination string and NULL terminate it?第三个问题:我所有的字符串都是 100 的固定长度。如果我使用上面的 3 个 strcpy() 示例,我的字符串不超过 99 个字符,strcpy() 是否正确覆盖目标字符串并 NULL 终止它? Meaning do I run into problems when using functions like strlen(), printf() later?这意味着我以后在使用 strlen()、printf() 等函数时会遇到问题吗?

Fourth question: What happens when I strcpy() empty strings?第四个问题:当我 strcpy() 空字符串时会发生什么?

I plan to overwrite these strings in loops various times and would like to know if it would be safer to use memset() to fully "empty" the strings prior to strcpy() on every iteration.我计划在循环中多次覆盖这些字符串,并想知道在每次迭代中使用 memset() 在 strcpy() 之前完全“清空”字符串是否更安全。

Thx.谢谢。

Are the global defined strings all initiated with 100 times '\\0'?全局定义的字符串是否都以 100 次 '\\0' 开头?

Yes.是的。 Global char arrays will be initilizated to all zeros.全局字符数组将初始化为全零。

I am a bit confused as to how exactly strcpy() works.我对 strcpy() 的工作原理有点困惑。 Does it only overwrite the characters necessary to place the source string into the destination string plus a \\0 at the end and leave the rest as it它是否只覆盖将源字符串放入目标字符串所需的字符加上末尾的 \\0 并将其余字符保留为它

Exactly.确切地。 It copies the characters up until and including '\\0' and does not care about the rest.它复制字符直到并包括 '\\0' 并且不关心其余的。

If I use ... my strings not exceeding 99 characters, does strcpy() properly overwrite the destination string and NULL terminate it?如果我使用...我的字符串不超过 99 个字符,strcpy() 是否正确覆盖目标字符串并 NULL 终止它?

Yes, but NULL is a pointer, it's terminated with zero byte, sometimes called NUL .是的,但NULL是一个指针,它以零字节结尾,有时称为NUL You might want to see What is the difference between NUL and NULL?您可能想看看NUL 和 NULL 之间有什么区别? . .

Meaning do I run into problems when using functions like strlen(), printf() later?这意味着我以后在使用 strlen()、printf() 等函数时会遇到问题吗?

Not if your string lengths are less than or equal to 99.如果您的字符串长度小于或等于 99,则不会。

What happens when I strcpy() empty strings?当我 strcpy() 空字符串时会发生什么?

It just copies one zero byte.它只是复制一个零字节。

would like to know if it would be safer to use memset() to fully "empty" the strings prior to strcpy() on every iteration.想知道在每次迭代中使用 memset() 在 strcpy() 之前完全“清空”字符串是否更安全。

Safety is a broad concept.安全是一个宽泛的概念。 As far as safety as in if the program will execute properly, there is no point in caring about anything after zero byte, so just strcpy it.至于程序是否正确执行的安全性,在零字节之后没有任何意义,所以只需对其进行strcpy

But you should check if your strings are less than 99 characters and handle what to do it they are longer.但是您应该检查您的字符串是否少于99字符并处理它们更长的操作。 You might be interested in strnlen , but the interface is confusing - I recommend to use memcpy + explicitly manually set zero byte.您可能对strnlen感兴趣,但界面令人困惑 - 我建议使用memcpy + 显式手动设置零字节。

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

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