A wide character string (WCS) is a null-terminated array of wchar_t values, with one wide character representing each character of an extended character set. It provides a wide-character counterpart to an ordinary char string. section 14.3
Converting a multibyte string
The mbsrtowcs function converts a multibyte string into a WCS. A dynamically allocated result needs space for the converted characters and its terminating wide null character; invalid input must be handled before conversion. section 14.3
Working with wide character strings involves these distinctions:
- Use wcs-prefixed functions for operations that ordinary str functions do not perform correctly on wide character strings, including counting, searching, and matching. section 14.3
- Treat wchar_t encoding as implementation-dependent; modern platforms commonly use 16- or 32-bit representations, while Unicode support can be checked with implementation macros. section 14.3
wchar_t* mbsrdup(char const*s, mbstate_t*restrict state) {
mbstate_t st = state ? *state : *MBSTATE;
size_t mblen = mbsrlen(s, &st);
if (mblen == mbinvalid) return nullptr;
wchar_t* S = malloc(sizeof(wchar_t[mblen+1]));
/* We know that s converts well, so no error check */
if (S) mbsrtowcs(S, &s, mblen+1, state);
return S;
}
Inspect a complete conversion pattern that measures a multibyte string, allocates a wchar_t array, and converts the data. section 14.3

