liveBook · Manning

Wide character string

As explained in Modern C

A wide character string is a null-terminated array of wchar_t elements representing a sequence of wide characters.

A wide character string is a null-terminated array of wchar_t elements. It represents a sequence of wide characters rather than a sequence of char bytes. That distinction matters when a multibyte string contains characters that occupy more than one byte: strlen counts bytes, not wide characters. section 14.3

TAKEAWAY: Count or convert multibyte characters before allocating a wide character string; allow one additional wchar_t element for the terminator. Conversion may require an mbstate_t parsing state. section 14.3

38   wchar_t* mbsrdup(char const*s, mbstate_t const*restrict state) {
39     size_t mblen = mbsrlen(s, state);
40     if (mblen == -1) return 0;
41     mbstate_t st = state ? *state : *MBSTATE;
42     wchar_t* S = malloc(sizeof(wchar_t[mblen+1]));
43     /* We know that s converts well, so no error check */ 
44     if (S) mbsrtowcs(S, &s, mblen+1, &st);
45     return S;
46   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":14}],[{\"line\":0,\"ch\":22},{\"line\":0,\"ch\":32}],[{\"line\":0,\"ch\":36},{\"line\":0,\"ch\":45}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":16}],[{\"line\":0,\"ch\":27},{\"line\":0,\"ch\":32}],[{\"line\":0,\"ch\":52},{\"line\":0,\"ch\":60}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":13}],[{\"line\":1,\"ch\":14},{\"line\":1,\"ch\":19}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":16}],[{\"line\":4,\"ch\":42},{\"line\":4,\"ch\":47}],[{\"line\":6,\"ch\":31},{\"line\":6,\"ch\":36}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":9}],[{\"line\":6,\"ch\":7},{\"line\":6,\"ch\":9}],[{\"line\":1,\"ch\":14},{\"line\":1,\"ch\":19}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":16}],[{\"line\":4,\"ch\":42},{\"line\":4,\"ch\":47}],[{\"line\":6,\"ch\":31},{\"line\":6,\"ch\":36}],[{\"line\":2,\"ch\":24},{\"line\":2,\"ch\":30}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":13}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":0,\"ch\":36},{\"line\":0,\"ch\":45}],[{\"line\":3,\"ch\":7},{\"line\":3,\"ch\":16}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":14}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":26}],[{\"line\":4,\"ch\":27},{\"line\":4,\"ch\":33}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":12}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":14}],[{\"line\":1,\"ch\":14},{\"line\":1,\"ch\":19}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":16}],[{\"line\":4,\"ch\":42},{\"line\":4,\"ch\":47}],[{\"line\":6,\"ch\":31},{\"line\":6,\"ch\":36}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}],[{\"line\":6,\"ch\":0},{\"line\":6,\"ch\":2}],[{\"line\":2,\"ch\":7},{\"line\":2,\"ch\":9}],[{\"line\":6,\"ch\":7},{\"line\":6,\"ch\":9}],[{\"line\":6,\"ch\":14},{\"line\":6,\"ch\":23}],[{\"line\":1,\"ch\":14},{\"line\":1,\"ch\":19}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":16}],[{\"line\":4,\"ch\":42},{\"line\":4,\"ch\":47}],[{\"line\":6,\"ch\":31},{\"line\":6,\"ch\":36}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":2}],[{\"line\":2,\"ch\":24},{\"line\":2,\"ch\":30}],[{\"line\":7,\"ch\":7},{\"line\":7,\"ch\":13}],[{\"line\":8,\"ch\":0},{\"line\":8,\"ch\":2}]]"}
!@%STYLE%@!

The conversion example obtains a wide-character count, allocates mblen + 1 wchar_t elements, then calls mbsrtowcs to fill the array. section 14.3

How Modern C covers it

How deep it goes
Works through length calculation and allocation for conversion, without developing a general wide-string interface.
Examples
Uses a conversion function that allocates mblen + 1 wchar_t elements and calls mbsrtowcs.
What it assumes
Assumes familiarity with char strings and C allocation while introducing wchar_t and multibyte parsing state in context.
What you can do after
Build it from scratch by counting multibyte characters, allocating space for a terminator, and converting with mbsrtowcs.
How it connects
Connects wide strings to multibyte strings, wchar_t, mbstate_t, and mbsrtowcs.

Also explained in

Compare how 2 books explain wide character string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.