Home > Software design >  Properties of strcpy()
Properties of strcpy()

Time:11-15

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(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'?

Second qestion: I am a bit confused as to how exactly strcpy() works. 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?

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? Meaning do I run into problems when using functions like strlen(), printf() later?

Fourth question: What happens when I strcpy() empty strings?

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.

Thx.

CodePudding user response:

Are the global defined strings all initiated with 100 times '\0'?

Yes. Global char arrays will be initilizated to all zeros.

I am a bit confused as to how exactly strcpy() works. 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

Exactly. It copies the characters up until and including '\0' and does not care about the rest.

If I use ... my strings not exceeding 99 characters, does strcpy() properly overwrite the destination string and NULL terminate it?

Yes, but NULL is a pointer, it's terminated with zero byte, sometimes called NUL. You might want to see What is the difference between NUL and NULL? .

Meaning do I run into problems when using functions like strlen(), printf() later?

Not if your string lengths are less than or equal to 99.

What happens when I strcpy() empty strings?

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.

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.

But you should check if your strings are less than 99 characters and handle what to do it they are longer. You might be interested in strnlen, but the interface is confusing - I recommend to use memcpy explicitly manually set zero byte.

  • Related