Why doesn't compound literals assignment work without a typecast

☆樱花仙子☆ 提交于 2019-12-01 20:54:19

问题


I have a question about literals in 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?


回答1:


(struct point){5,6} as a whole is a compound literal.

C11 §6.5.2.5 Compound literals

A postfix expression that consists of a parenthesized type name followed by a brace enclosed list of initializers is a compound literal.



来源:https://stackoverflow.com/questions/25603617/why-doesnt-compound-literals-assignment-work-without-a-typecast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!