c - 如何将结构复合文字作为参数传递给函数?
问题描述
这是struct
我所拥有的:
typedef struct
{
float r, g, b;
} color_t;
我想将 this 的复合文字struct
作为参数传递给函数,如下所示:
void printcolor(color_t c)
{
printf("color is : %f %f %f\n", c.r, c.g, c.b);
}
printcolor({1.0f, 0.6f, 0.8f});
但是,这给了我一个错误:
错误:“{”标记之前的预期表达式
解决方案
C 中的复合文字必须在大括号括起来的初始值设定项列表之前具有指定的类型(使用'cast-like'语法)。因此,如评论中所述,您应该将函数调用更改为:
printcolor((color_t){1.0f, 0.6f, 0.8f});
推荐阅读
- reactjs - 变量状态未定义,但它是
- java - 用于查找不以特定子字符串结尾的数字的 Java 模式正则表达式
- php - 如何记录不存在的用户?- 拉拉维尔
- java - 对象映射器反序列化映射数组
- error-handling - 如果实体已存在,则返回案例中的实体 - API 平台
- yii2 - ActiveRecord 和 GridView 行为中的 Yii2 多态关系
- java - 如何从 Java 8 orElse/orElseGet 运算符返回 HashMap
- c++ - 为什么 Visual Studio 无法在模板类中选择正确的构造函数?
- reactjs - 将电子应用程序发布到 Windows 商店时如何修复“可用的应用程序图标包括默认图标”?
- android - 导航应该弹回堆栈而不是返回起始目的地