While reading the man page for strcpy
, I discovered the function stpcpy
also exists. However, the only difference I could notice in the man page is:
char * stpcpy(char *s1, const char *s2); char * strcpy(char *restrict s1, const char *restrict s2);
So, what does restrict
means here?
The strcpy() function copies one string into another. The strcat() function concatenates two functions.
The strcpy function copies string str2 into array str1 and returns the value of str1 . On the other hand, strncpy copies the n characters of string str2 into array str1 and returns the value of str1 .
Problem with strcpy(): The strcpy() function does not specify the size of the destination array, so buffer overrun is often a risk. Using strcpy() function to copy a large character array into a smaller one is dangerous, but if the string will fit, then it will not be worth the risk.
std::strcpy Copies the character string pointed to by src , including the null terminator, to the character array whose first element is pointed to by dest .
The question you are asking in the title and the question about restrict
are actually two different totally unrelated questions. The other answers have already provided you with some good links that will help you to learn more about restrict
.
However, the main difference between these two functons in not really the restrict
specifier. In fact, in C99 version of C language specification, strcpy
also has restrict
qualification on its parameters. What you see on your man page for strcpy
is simply not updated to conform to C99.
The main difference (which you seem to have missed) is the return value of stpcpy
. stpcpy
returns the pointer to the terminating \0
character of the target string. This immediately makes clear the purpose of stpcpy
and the rationale behind its existence: this function is intended to be used as an intelligent replacement to strcat
function in situations when you need to concatenate multiple sub-strings into one string. You see, strcat
works pretty poorly in such application (I'd even say that strcat
has no meaningful uses in real code). The problem with strcat
is that it rescans the destination string every time you add something to it, thus doing lots of unnecessary work and basically generating more heat than light. For example, the following code suffers from that problem
const char *part1, *part2, *part3, *part4; ... char buffer[size]; /* assume that `size` is calculated properly */ strcpy(buffer, part1); strcat(buffer, part2); strcat(buffer, part3); strcat(buffer, part4);
This code can be reimplemented in a much more reasonable way by using stpcpy
stpcpy(stpcpy(stpcpy(stpcpy(buffer, part1), part2), part3), part4);
And if you don't like chained calls, you can use an intermediate pointer to store the return value of intermediate stpcpy
calls
char *end = buffer; end = stpcpy(end, part1); end = stpcpy(end, part2); end = stpcpy(end, part3); end = stpcpy(end, part4);
Of course it is worth mentioning that strcpy
and strcat
are standard functions, while stpcpy
is not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With