The strcpy_s function is used to safely copy the string src to the string dst. Its syntax is: check the validity of the parameters. Call strcpy_s to copy src into dst. Check the return value to ensure that the copy was successful or that the target string was too small.
Usage of strcpy_s function in C
strcpy_s function is used to copy one string to another character string, it is better than the strcpy function in safely copying strings.
Syntax:
<code class="cpp">errno_t strcpy_s(char* dst, size_t dstSize, const char* src);</code>
Parameters:
Return value:
Usage:
dst
and src
Points to a valid string and dstSize
is large enough to accommodate the source string. dstSize
needs to be adjusted. Example:
<code class="cpp">char dst[100]; const char* src = "Hello, world!"; strcpy_s(dst, sizeof(dst), src); std::cout << dst << std::endl; // 输出 "Hello, world!"</code>
Note:
dst
is nullptr, src
is nullptr, or dstSize
is 0, then strcpy_s
will return EINVAL. dstSize
The parameter must include the null character, otherwise strcpy_s
may cause a buffer overflow. The above is the detailed content of How to use strcpy_s in c++. For more information, please follow other related articles on the PHP Chinese website!