简体   繁体   English

C:为什么我不能用花括号初始化数组内部的结构?

[英]C: Why can I not initialize structs inside of arrays with curly brackets?

I have the following code: 我有以下代码:

struct coord {
    float x;
    float y;
};

struct coord vertices[4];

But when I do the following, an error occurs: 但是当我执行以下操作时,会发生错误:

void setVertices(float x, float y) {
    vertices[0] = (struct coord) { x, y };
}

Why isn't this allowed in C? 为什么在C中不允许这样做? Is there another way to do this? 还有另一种方法吗? Thank you. 谢谢。

I can initialize structs that way, actually. 实际上,我可以用这种方式初始化结构。 The IDE just lagged on its error-checking for several minutes, for some reason. 由于某种原因,IDE仅在几分钟内就进行了错误检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM