| 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*strncpy(char*dest,constchar*src,size_t count); | (until C99) | |
char*strncpy(char*restrict dest,constchar*restrict src,size_t count); | (since C99) | |
errno_t strncpy_s(char*restrict dest, rsize_t destsz, constchar*restrict src, rsize_t count); | (2) | (since C11) |
count characters of the character array pointed to bysrc (including the terminating null character, but not any of the characters that follow the null character) to character array pointed to bydest.count is reached before the entire arraysrc was copied, the resulting character array is not null-terminated.src,count is not reached, additional null characters are written todest until the total ofcount characters have been written.dest orsrc is not a pointer to a character array (including ifdest orsrc is a null pointer), if the size of the array pointed to bydest is less thancount, or if the size of the array pointed to bysrc is less thancount and it does not contain a null character.count, it stops after writing the terminating null character (if there was no null in the source, it writes one atdest[count] and then stops). Also, the following errors are detected at runtime and call the currently installedconstraint handler function:src ordest is a null pointerdestsz is zero or greater thanRSIZE_MAXcount is greater thanRSIZE_MAXcount is greater or equaldestsz, butdestsz is less or equalstrnlen_s(src, count), in other words, truncation would occurdest <strnlen_s(src, destsz) <=destsz; in other words, an erroneous value ofdestsz does not expose the impending buffer overflow. The behavior is undefined if the size of the character array pointed to bysrc <strnlen_s(src, count) <destsz; in other words, an erroneous value ofcount may permit buffer overflow.strncpy_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 copy to |
| src | - | pointer to the character array to copy from |
| count | - | maximum number of characters to copy |
| destsz | - | the size of the destination buffer |
destdest is a null pointer ordestsz is zero or greater thanRSIZE_MAX) and may clobber the rest of the destination array with unspecified values.As corrected by the post-C11 DR 468,strncpy_s, unlikestrcpy_s, is only allowed to clobber the remainder of the destination array if an error occurs.
Unlikestrncpy,strncpy_s does not pad the destination array with zeroes, This is a common source of errors when converting existing code to the bounds-checked version.
Although truncation to fit the destination buffer is a security risk and therefore a runtime constraints violation forstrncpy_s, it is possible to get the truncating behavior by specifyingcount equal to the size of the destination array minus one: it will copy the firstcount bytes and append the null terminator as always:strncpy_s(dst,sizeof dst, src,(sizeof dst)-1);
#define __STDC_WANT_LIB_EXT1__ 1#include <string.h>#include <stdio.h>#include <stdlib.h>#include <errno.h> int main(void){char src[]="hi";char dest[6]="abcdef";// no null terminator strncpy(dest, src,5);// writes five characters 'h', 'i', '\0', '\0', '\0' to destprintf("strncpy(dest, src, 5) to a 6-byte dest gives : ");for(size_t n=0; n<sizeof dest;++n){char c= dest[n]; c?printf("'%c' ", c):printf("'\\0' ");} printf("\nstrncpy(dest2, src, 2) to a 2-byte dst gives : ");char dest2[2]; strncpy(dest2, src,2);// truncation: writes two characters 'h', 'i', to dest2for(size_t n=0; n<sizeof dest2;++n){char c= dest2[n]; c?printf("'%c' ", c):printf("'\\0' ");}printf("\n"); #ifdef __STDC_LIB_EXT1__ set_constraint_handler_s(ignore_handler_s);char dst1[6], src1[100]="hello"; errno_t r1= strncpy_s(dst1,6, src1,100);// writes 0 to r1, 6 characters to dst1printf("dst1 =\"%s\", r1 = %d\n", dst1,r1);// 'h','e','l','l','o','\0' to dst1 char dst2[5], src2[7]={'g','o','o','d','b','y','e'}; errno_t r2= strncpy_s(dst2,5, src2,7);// copy overflows the destination arrayprintf("dst2 =\"%s\", r2 = %d\n", dst2,r2);// writes nonzero to r2,'\0' to dst2[0] char dst3[5]; errno_t r3= strncpy_s(dst3,5, src2,4);// writes 0 to r3, 5 characters to dst3printf("dst3 =\"%s\", r3 = %d\n", dst3,r3);// 'g', 'o', 'o', 'd', '\0' to dst3#endif}
Possible output:
strncpy(dest, src, 5) to a 6-byte dst gives : 'h' 'i' '\0' '\0' '\0' 'f'strncpy(dest2, src, 2) to a 2-byte dst gives : 'h' 'i'dst1 = "hello", r1 = 0dst2 = "", r2 = 22dst3 = "good", r3 = 0
(C11) | copies one string to another (function)[edit] |
(C11) | copies one buffer to another (function)[edit] |
(dynamic memory TR) | allocate a copy of a string up to specified size (function)[edit] |
C++ documentation forstrncpy | |