【发布时间】:2017-01-04 11:11:12
【问题描述】:
根据this question 中的答案,像L"test" 这样的文字具有wchar_t[5] 类型。但是下面的 GCC 代码似乎表达了不同的意思:
int main()
{
struct Test{char x;} s;
s="Test"; // ok, char* as expected
s=L"Test"; // ??? I'd expect wchar_t*
return 0;
}
这是我编译它的方式(gcc 5.2,但 4.5 和 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*
^
显然,我得到的不是wchar_t 的预期数组,而是long int 的数组。这里有什么问题?
【问题讨论】:
-
但是在 Linux 上 wchar_t 是 32 位的,在某些架构中它甚至可能只是 long int 的 typedef。
-
struct Test{} s;没有意义。结构必须至少有一个成员。请提供可编译的代码。 -
wchar_t不是内置类型。它是一种实现定义的类型,恰好相当于您系统上的long int。在我的系统上,它只是一个普通的int。 -
@2501 如果我提供编译的代码,我永远不会找出文字的类型。它是故意设计的,目的是给出一个有意义的错误。
-
在我的窗口上我得到
...assigning to type 'struct Test' from type 'short unsigned int *'
标签: c string-literals wchar-t