为什么在没有类型转换的情况下复合文字分配工作

时间:2014-09-01 10:19:43

标签: c c99 compound-literals

我对C中的文字有疑问。

int a;
//a is an integer that is assigned an integer literal 414
a = 414;

float b;
//b is a float that is assigned a float literal of 3.14
b = 3.14;

struct point {
    int x,y;
};

struct point b;
//{5,6} is a compound literal that is assigned to a struture.
b = {5,6}; //doesn't work.

b = (struct point){5,6}; //works.

没有类型转换似乎没有用?这是什么原因?

1 个答案:

答案 0 :(得分:5)

(struct point){5,6}整体上是复合文字。

  

C11§6.5.2.5复合文字

     

后缀表达式,由带括号的类型名称后跟括号括起来组成   初始化列表是复合文字。