strncpy, wcsncpy (Windows CE 5.0)
Developing an Application > Microsoft C Run-time Library for Windows CE > Run-time Library Reference
Copy characters of one string to another.
char *strncpy( char *strDest, constchar *strSource, size_tcount);wchar_t *wcsncpy( wchar_t *strDest, constwchar_t *strSource, size_tcount);
Parameters
- strDest
Destination string. - strSource
Source string. - count
Number of characters to be copied.
Return Values
Each of these functions returns strDest. No return value is reserved to indicate an error.
Remarks
These functions are supported by all versions of the C run-time libraries.
The strncpy function copies the initial count characters of strSource to strDest and returns strDest.
If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string.
If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.
wcsncpy is the wide-character version of strncpy. The arguments and return value of wcsncpy vary accordingly. These two functions behave identically otherwise.
The first argument, strDest, must be large enough to hold strSource and the closing '\0'; otherwise, a buffer overrun can occur.
This can lead to a denial of service attack against the application if an access violation occurs, or in the worst case, allow an attacker to inject executable code into your process. This is especially true if strDest is a stack-based buffer. Consider using an appropriate strsafe function.
For more information, see Safe String Functions.
The following table shows generic-text routine mappings for this function.
TCHAR.H Routine | _UNICODE Defined |
---|---|
_tcsncpy | wcsncpy |
For more information about TCHAR.H routines, see Generic Text Mappings.
Example
Description
The following example copies one string to another.
Code
#include <string.h>
#include <stdio.h>
void main( void )
{
char string[100] = "Cats are nice usually";
printf ( "Before: %s\n", string );
strncpy( string, "Dogs", 4 );
strncpy( string + 9, "mean", 4 );
printf ( "After: %s\n", string );
}
// Output
Before: Cats are nice usually
After: Dogs are mean usually
Requirements
OS Versions: Windows CE 2.0 and later.
Header: stdio.h, string.h.
Link Library: coredll.dll.
See Also
strcmp | strcpy | strncat | strncmp
Send Feedback on this topic to the authors