Search code examples
cwinapiolewidechar

Append extra null char to wide string


Some Win32 API structures require to concatenate an extra null character to a string, as in the following example taken from here:

c:\temp1.txt'\0'c:\temp2.txt'\0''\0'

When it comes to wide strings, what is the easiest way to append a L'\0' to the end of an existing wide string?

Here's what works for me but seems too cumbersome:

wchar_t my_string[10] = L"abc";
size_t len = wcslen(my_string);
wchar_t nullchar[1] = {'\0'};
memcpy(my_string + len + 1, nullchar, sizeof(wchar_t));

Solution

  • assuming my_string is long enough:

    my_string[wcslen(my_string)+1]='\0';
    

    The terminating null will be translated to a wide char.

    (Posted as a first comment to the question)