strncpy
- Updated2023-02-21
- 2 minute(s) read
strncpy
char *strncpy (char targetString[], const char sourceString[], size_t maxChars);
Purpose
Copies characters from a source string to a target string until it reaches the specified number of characters or an ASCII NUL byte. If an ASCII NUL byte is found in the source buffer within the specified number of bytes, NUL bytes are written to the target until the specified number of characters have been filled in. If no ASCII NUL byte is found within the specified number of bytes, the function returns after copying the specified number of bytes and does not append an ASCII NUL byte to the buffer. If you want to guarantee that an ASCII NUL byte is at the end of the copied bytes, you can use the following:
strncpy (target, source, n);target[n] = 0;
If copying takes place between two objects that overlap, the behavior is undefined.
Parameters
Input | ||
Name | Type | Description |
sourceString | const char [] | Contains a pointer to the NUL-terminated source string from which a specified number of characters are copied into the target string. |
maxChars | size_t |
Specifies the maximum number of characters that are copied from the source string into the target string. If the source string
is shorter than the length specified in this parameter, ASCII NUL bytes are appended to the target string until
maxChars are written. If the source string is longer than the length specified in this parameter,
no ASCII NUL bytes are written to the target buffer. If you want to guarantee that an ASCII NUL byte is at the end of the
copied bytes, you can use the following:
strncpy (target, source, n); target[n] = 0; |
Output | ||
Name | Type | Description |
targetString | char [] |
Contains the target string to which the specified number of characters from the source string are copied. If the source string is
shorter than maxChars, ASCII NUL bytes are appended to the contents of this parameter until maxChars
are written. If the source string is longer than maxChars, no ASCII NUL bytes are written to the contents of this
parameter. If you want to guarantee that an ASCII NUL byte is at the end of the copied bytes, you can use the following:
strncpy (target, source, n); target[n] = 0; |
Return Value
Name | Type | Description |
returnedTargetString | char * | Contains a pointer to the modified target string. |
Additional Information
Library: ANSI C Library
Include file: ansi_c.h
LabWindows/CVI compatibility: LabWindows/CVI 3.0 and later
Example
Refer to userint\DigGraphDAQmx.cws for an example of using the strncpy function.