简体   繁体   English

如果没有类型转换,为什么复合文字赋值不起作用

[英]Why doesn't compound literals assignment work without a typecast

I have a question about literals in C. 我对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.

That doesn't seem to work without a typecast? 没有类型转换似乎没有用? What is the reason for this? 这是什么原因?

(struct point){5,6} as a whole is a compound literal. (struct point){5,6}作为一个整体是复合文字。

C11 §6.5.2.5 Compound literals C11§6.5.2.5 复合文字

A postfix expression that consists of a parenthesized type name followed by a brace enclosed list of initializers is a compound literal. 后缀表达式由带括号的类型名称后跟括号括起的初始值设定项列表组成,是一个复合文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM