| Functions | |||||||||||||||||||||||||||||||||||||||||
| Character manipulation | |||||||||||||||||||||||||||||||||||||||||
| Conversions to and from numeric formats | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String manipulation | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
| String examination | |||||||||||||||||||||||||||||||||||||||||
| Memory manipulation | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| Miscellaneous | |||||||||||||||||||||||||||||||||||||||||
(C11)(C11) | |||||||||||||||||||||||||||||||||||||||||
Defined in header <string.h> | ||
| (1) | ||
char* strcpy(char* dest,constchar* src); | (until C99) | |
char* strcpy(char*restrict dest,constchar*restrict src); | (since C99) | |
errno_t strcpy_s(char*restrict dest, rsize_t destsz,constchar*restrict src); | (2) | (since C11) |
strcpy_s is only guaranteed to be available if__STDC_LIB_EXT1__ is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__ to the integer constant1 before including<string.h>.Contents |
| dest | - | pointer to the character array to write to |
| src | - | pointer to the null-terminated byte string to copy from |
| destsz | - | maximum number of characters to write, typically the size of the destination buffer |
strcpy_s is allowed to clobber the destination array from the last character written up todestsz in order to improve efficiency: it may copy in multibyte blocks and then check for null bytes.
The functionstrcpy_s is similar to the BSD functionstrlcpy, except that
strlcpy truncates the source string to fit in the destination (which is a security risk)strlcpy does not perform all the runtime checks thatstrcpy_s doesstrlcpy does not make failures obvious by setting the destination to a null string or calling a handler if the call fails.Althoughstrcpy_s prohibits truncation due to potential security risks, it's possible to truncate a string using bounds-checkedstrncpy_s instead.
#define __STDC_WANT_LIB_EXT1__ 1#include <stdio.h>#include <stdlib.h>#include <string.h> int main(void){constchar* src="Take the test.";// src[0] = 'M' ; // this would be undefined behaviorchar dst[strlen(src)+1];// +1 to accommodate for the null terminator strcpy(dst, src); dst[0]='M';// OKprintf("src = %s\ndst = %s\n", src, dst); #ifdef __STDC_LIB_EXT1__ set_constraint_handler_s(ignore_handler_s);int r= strcpy_s(dst,sizeof dst, src);printf("dst =\"%s\", r = %d\n", dst, r); r= strcpy_s(dst,sizeof dst,"Take even more tests.");printf("dst =\"%s\", r = %d\n", dst, r);#endif}
Possible output:
src = Take the test.dst = Make the test.dst = "Take the test.", r = 0dst = "", r = 22
(C11) | copies a certain amount of characters from one string to another (function)[edit] |
(C11) | copies one buffer to another (function)[edit] |
(C95)(C11) | copies one wide string to another (function)[edit] |
(dynamic memory TR) | allocate a copy of a string (function)[edit] |
C++ documentation forstrcpy | |