长字符串文字的类型是长int*吗?

时间:2022-11-20 22:27:50

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:

根据这个问题的答案,像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;
}

Here's how I compile it (gcc 5.2, but same results are with 4.5 and 4.8):

下面是我如何编译它(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*
      ^

Apparently, instead of the expected array of wchar_t I get array of long int. What's wrong here?

显然,我得到的不是wchar_t的期望数组,而是长整数数组,这里有什么问题吗?

1 个解决方案

#1


2  

The type wchar_t is not a fundamental type, like char. It is an implementation-defined synonym of an integer type1.

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


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;

1(引用自:ISO/ iec989: 201x7.19通用定义2)wchar_t是一个整数类型,它的值范围可以代表受支持的区域内指定的最大扩展字符集的所有成员的不同代码;

#1


2  

The type wchar_t is not a fundamental type, like char. It is an implementation-defined synonym of an integer type1.

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


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;

1(引用自:ISO/ iec989: 201x7.19通用定义2)wchar_t是一个整数类型,它的值范围可以代表受支持的区域内指定的最大扩展字符集的所有成员的不同代码;