According to the answers in this question, a literal like L"test"
has type wchar_t[5]
. But the following code with GCC seems to say something different:
int main()
{
struct Test{char x;} s;
s="Test"; // ok, char* as expected
s=L"Test"; // ??? I'd expect wchar_t*
return 0;
}
Here's how I compile it (gcc 5.2, but same results are with 4.5 and 4.8):
$ gcc test.c -o test -std=c99
test.c: In function ‘main’:
test.c:4:6: error: incompatible types when assigning to type ‘struct Test’ from type ‘char *’
s="Test"; // ok, char* as expected
^
test.c:5:6: error: incompatible types when assigning to type ‘struct Test’ from type ‘long int *’
s=L"Test"; // ??? I'd expect wchar_t*
^
Apparently, instead of the expected array of wchar_t
I get array of long int
. What's wrong here?
The type wchar_t is not a fundamental type, like char. It is an implementation-defined synonym of an integer type1.
1 (Quoted from: ISO/IEC 9899:201x 7.19 Common definitions 2.)
wchar_t which is an integer type whose range of values can represent distinct codes for all
members of the largest extended character set specified among the supported locales;