gpt4 book ai didi

c - 如何将结构复合文字作为参数传递给函数?

转载 作者:行者123 更新时间:2023-12-03 16:31:10 24 4
gpt4 key购买 nike

这是一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com