wcstombs, wcstombs_s
From cppreference.com
Defined in header <stdlib.h>
|
||
(1) | ||
(until C99) | ||
(since C99) | ||
errno_t wcstombs_s( size_t *restrict retval, char *restrict dst, rsize_t dstsz, const wchar_t *restrict src, rsize_t len ); |
(2) | (since C11) |
1) Converts a sequence of wide characters from the array whose first element is pointed to by
src
to its narrow multibyte representation that begins in the initial shift state. Converted characters are stored in the successive elements of the char array pointed to by dst
. No more than len
bytes are written to the destination array. Each character is converted as if by a call to wctomb, except that the wctomb's conversion state is unaffected. The conversion stops if:
* The null character L'\0' was converted and stored. The bytes stored in this case are the unshift sequence (if necessary) followed by '\0',
* A wchar_t was found that does not correspond to a valid character in the current C locale.
* The next multibyte character to be stored would exceed
len
. If
src
and dst
overlap, the behavior is unspecified.2) Same as (1), except that
* the function returns its result as an out-parameter
retval
* if the conversion stops without writing a null character, the function will store '\0' in the next byte in
dst
, which may be dst[len]
or dst[dstsz]]
, whichever comes first (meaning up to len+1/dstsz+1 total bytes may be written). In this case, there may be no unshift sequence written before the terminating null. * if
dst
is a null pointer, the number of wide characters that would be produced is stored in *retval * the function clobbers the destination array from the terminating null and until
dstsz
* If
src
and dst
overlap, the behavior is unspecified. * the following errors are detected at runtime and call the currently installed constraint handler function:
-
retval
orsrc
is a null pointer -
dstsz
orlen
is greater than RSIZE_MAX (unlessdst
is null) -
dstsz
is not zero (unlessdst
is null) -
len
is greater thandstsz
and the conversion does not encounter null or encoding error in thesrc
array by the timedstsz
is reached (unlessdst
is null)
-
- As all bounds-checked functions,
wcstombs_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 constant 1 before includingstdlib.h
.
Notes
In most implementations, wcstombs
updates a global static object of type mbstate_t as it processes through the string, and cannot be called simultaneously by two threads, wcsrtombs or wcstombs_s
should be used in such cases.
POSIX specifies a common extension: if dst
is a null pointer, this function returns the number of bytes that would be written to dst
, if converted. Similar behavior is standard for wcsrtombs and wcstombs_s
.
Parameters
dst | - | pointer to narrow character array where the multibyte character will be stored |
src | - | pointer to the first element of a null-terminated wide string to convert |
len | - | number of bytes available in the array pointed to by dst |
dstsz | - | max number of bytes that will be written (size of the dst array)
|
retval | - | pointer to a size_t object where the result will be stored |
Return value
1) On success, returns the number of bytes (including any shift sequences, but excluding the terminating '\0') written to the character array whose first element is pointed to by
dst
. On conversion error (if invalid wide character was encountered), returns (size_t)-1.2) Returns zero on success (in which case the number of bytes excluding terminating zero that were, or would be written to
dst
, is stored in *retval), non-zero on error. In case of a runtime constraint violation, stores (size_t)-1 in *retval (unless retval
is null) and sets dst[0] to '\0' (unless dst
is null or dstmax
is zero or greater than RSIZE_MAX)