strncpy_s, wcsncpy_s
Go Up to string.h Index
Header File
string.h, mbstring.h
Category
Memory and String Manipulation Routines, Inline Routines
Prototype
errno_t strncpy_s(char * _RESTRICT dest, rsize_t destmax, const char * _RESTRICT src, rsize_t n);
errno_t wcsncpy_s(wchar_t * _RESTRICT dest, rsize_t destmax, const wchar_t * _RESTRICT src, rsize_t n);
Description
Replaces Strncpy, mbsncpy, wcsncpy adding security enhancements.
Copies a given number of bytes from one string into another, truncating or padding as necessary.
strncpy_s copies up to n characters from src into dest, truncating or null-padding dest. Characters that come after '\0' are not copied. The target string, dest, might not be null-terminated if the length of src is destmax or more.
If there is a run-time constraint violation or no character was copied from src, dest[0] is set to null.
Return Value
strncpy_S returns dest.
Example
#include <stdio.h> #include <string.h> int main(void) { char string[10]; char *str1 = "abcdefghi"; strncpy_s(string, 10, str1, 3); string[3] = '\0'; printf("%s\n", string); return 0; }
Portability
POSIX | Win32 | ANSI C | ANSI C++ | |
---|---|---|---|---|
strncpy_s |
+ |
+ |
+ |
+ |