简体   繁体   English

C:数组结构作为结构数组

[英]C: Struct of arrays as array of structs

I was wondering wondering if it was possible to read a struct of arrays as an arry of structs.我想知道是否可以将数组结构作为结构体来读取。

eg例如

typedf struct foo{
    int a[];
    int b[];
    int c[];
}foo_t;

main:
foo_t foo ={.a={11,12,13},.b={21,22,33},.c={31,32,33}};

So foo[0] would contain {11,21,31}, foo[1] would contain {12,22,32} and foo[2] would contain {13,23,33}所以 foo[0] 将包含 {11,21,31},foo[1] 将包含 {12,22,32} 而 foo[2] 将包含 {13,23,33}

any idea?任何的想法?

Why don't you use an array of the struct.为什么不使用结构数组。

typedef struct foo{
  int a;
  int b;
  int c;
}foo_t;

main:
foo_t foo[] ={{11,21,31}, {12,22,32}, {13,23,33}};

So foo[0] would contain {11,21,31}, foo[1] would contain {12,22,32} and foo[2] would contain {13,23,33}所以 foo[0] 将包含 {11,21,31},foo[1] 将包含 {12,22,32} 而 foo[2] 将包含 {13,23,33}

No, you can't.不,你不能。 An array of structs is structurally and semantically different from a struct of arrays.结构数组在结构和语义上与数组结构不同。

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

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