C : array of struct (which holds an int and another array of another struct)

标签 c arrays struct

struct dataStruct {     const char* s;     int num; }; 

struct Final_struct {     int n;     dataStruct a[]; }; 

现在当我尝试如下初始化 Final_struct 时出现问题:

const Final_struct Example[]= {
                                {100, { {"age", 20}, {"iq", 120}, {"bmi",26} } },
                                {100, { {"age", 36}, {"iq", 145}, {"bmi",22} }}
};

这是一个 C 代码,当我尝试编译它时出现编译器错误:

对象的字段不能有大小为 0 的数组

有什么建议吗?

谢谢。

最佳答案

dataStruct a[] 将结构的成员定义为大小为 0 的数组。这实际上是无用的。您需要在 struct 的定义中指定其大小,因为编译器需要提前知道整个 struct 的大小。

或者,您可以简单地将字段声明为 dataStruct *a,然后数组本身将不包含在 struct 中。

关于C : array of struct (which holds an int and another array of another struct),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6303677/

相关文章:

c - sscanf 指针与变量地址

c++ - 将结构复制(使用赋值)到 union 内的结构导致段错误

json - 两个 Api 调用,具有不同的 JSON 响应。如何将它们输入到不同的结构中,但在 Golang 中重新排列它们几乎相同?

ios - 快速性能问题: local array vs global array

javascript - 遍历嵌套数组以匹配 ID

c - 自动存储持续时间结构初始化

c - Visual Studio C程序: How to print symbols for card suits?

c - 如何删除 CLOSE_WAIT 套接字连接?

C:生成具有范围内随机数的数组(允许重复)

javascript - 一次显示多个数据点: Highcharts