Hello friends,
Is there any problem with "Zero-Length-String" in C/C++.
Pls mention if there is any type of problem in standard string library-functions like strcpy, strcat, memcpy, etc.
Thanx,
Amit
Hello friends,
Is there any problem with "Zero-Length-String" in C/C++.
Pls mention if there is any type of problem in standard string library-functions like strcpy, strcat, memcpy, etc.
Thanx,
Amit
zero length strings are ok. You can use all the mentioned functions on zero length strings.
Note that a zero length string will actually have at least 1 byte (the '\0' terminator character) in memory for it; if it didn't, then the pointer to the 'string' would be invalid.
It really depends on what you mean by zero-length string and how you want to use it. If its the source string in the functions you mentioned then yes, they can be safely used there. But if they are the destination string, then safety will depend on how the string was declared.
char str[1] = 0; // zero-length string
strcpy(str, "Hello"); // this will crash your program
char* str1 = new char[255];
str[0] = 0; // make zero length string
strcpy(str1, "Hello"); // this is ok
strcat(str1,"Hello"); // this is ok too
char another_str[255] = {0}; // zero length string
strcpy(another_str, str); // this is ok
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.