这是一个 struct
我有:
typedef struct
{
float r, g, b;
} color_t;
我想传递这个 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});
但是,这给了我一个错误:error: expected expression before '{' token
最佳答案
compound literal在 C 中,必须在括号括起来的初始值设定项列表之前指定类型(使用 'cast-like' 语法)。因此,如评论中所述,您应该将函数调用更改为:
printcolor((color_t){1.0f, 0.6f, 0.8f});
关于c - 如何将结构复合文字作为参数传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66831515/