Which method is correct for Initializing a wchar_t string?
The magic is the encoding-prefix L
:
#include <wchar.h>
...
wchar_t m1[] = L"Hello World";
wchar_t m2[42] = L"Hello World";
wchar_t * pm = L"Hello World";
...
wcscat(m2, L" again");
pm = calloc(123, sizeof *pm);
wcspy(pm, L"bye");
See also the related part of the C11 Standard.
It really depends on what you want to do and how you use the data. If you need it globally, by all means, define a static array. If you only need it in a method, do the same in the method. If you want to pass the data around between functions, over a longer lifetime, malloc
the memory and use that.
However, your method III is wrong - it is an array of 100 wchar_t
pointers. If you want to create a 100 large wchar_t
array and a pointer, you need to use:
wchar_t message[100], *message_pointer;
Also, concerning terminology: you are only declaring a variable in the method I, you never assign anything to it.