c - 后缀表达式 (type-name){initializer-list}

标签 c language-lawyer compound-literals

6.5.2.5/4 部分提供了关于 ( type-name ) { initializer-list } 形式的 postfix-expression 的解释。在这里:

If the type name specifies an array of unknown size, the size is determined by the initializer list as specified in 6.7.9, and the type of the compound literal is that of the completed array type. Otherwise (when the type name specifies an object type), the type of the compound literal is that specified by the type name. In either case, the result is an lvalue.

我不明白 the type of the compound literal 这句话。文字怎么可能有类型呢? 复合字面量的类型是否表示对应未命名对象的类型?

例如

long long *longs = (long long []) {1333, 3123, 3, 122};

此处的initializer-list用于初始化long long [4]类型的无名对象。

也不清楚的目的是什么,在任何一种情况下,结果都是左值。在 assignment-expression 中使用时,lvalue conversion 是在右操作数上执行的,因此它不再是左值。

最佳答案

据推测,“复合文字”是指使用“复合文字”语言构造指定的对象/值。值/对象在 C 中有类型。

使复合字面量成为左值的目的是这样的代码 int *x = &(int){42}; *x = 43; 有效。它使复合文字的行为有点像匿名变量。 (不过,不完全是。与常规变量不同,复合文字不能有存储类说明符,我个人认为这是语言缺陷。)

关于c - 后缀表达式 (type-name){initializer-list},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57459199/

相关文章:

生成可能的有序对(最多可达一定数量)并查找该对的二元运算的最大值的代码

c++ - 在 c/c++ 中更改 Windows 服务的状态

c - 为返回函数的函数内联 typedef

c - 是否可以在不事先定义的情况下将结构变量作为函数参数传递?

c - 打印链表时最后一个元素值出现在第一个和最后一个位置

c++ - 严格别名和二进制 I/O

c++ - 将转发 lambda 转换为函数指针

c# - 从 IEnumerable 转换为 IEnumerable<object>

c - 为什么不能使用复合文字来分配已定义的固定大小的数组?

c - 每次将复合文字分配给循环中的指针时是否都会创建一个新对象?