【问题标题】:Is long string literal's type long int*?长字符串文字的类型是 long int* 吗?
【发布时间】: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


【解决方案1】:

类型 wchar_t 不是基本类型,如 char。它是整数类型的实现定义的同义词1


1(引自:ISO/IEC 9899:201x 7.19 通用定义 2。)
wchar_t 是一个整数类型,它的值范围可以代表所有不同的代码 支持的语言环境中指定的最大扩展字符集的成员;

【讨论】:

    猜你喜欢
    • 2018-04-14
    • 2018-12-30
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多