没有类型转换,为什么复合文字分配不起作用 [英] Why doesn't compound literals assignment work without a typecast

查看:116
本文介绍了没有类型转换,为什么复合文字分配不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C语言中的文字有疑问.

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?

推荐答案

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

C11§6.5.2.5复合文字

一个后缀表达式,该表达式由带括号的类型名称和大括号括起来 初始化程序列表是复合文字.

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.

这篇关于没有类型转换,为什么复合文字分配不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆