繁体   English   中英

如何在C ++中创建结构体的结构

[英]How to make a struct of structs in C++

结构可以包含其他结构吗?

我想创建一个包含四个其他结构的数组的结构。 这可能吗? 代码会是什么样的?

是的你可以。 例如,此struct S2包含四个S1对象的数组:

struct S1 { int a; };

struct S2
{
    S1 the_array[4];
};

当然,为什么不呢。

struct foo {
    struct {
        int a;
        char *b;
    } bar[4];
} baz;

baz.bar[1].a = 5;

是的,结构可以包含其他结构。 例如:

struct sample {
  int i;
  char c;
};

struct b {
  struct sample first;
  struct sample second;
};

暂无
暂无

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

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