简体   繁体   English

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

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

Can a struct contain other structs? 结构可以包含其他结构吗?

I would like to make a struct that holds an array of four other structs. 我想创建一个包含四个其他结构的数组的结构。 Is this possible? 这可能吗? What would the code look like? 代码会是什么样的?

Yes, you can. 是的你可以。 For example, this struct S2 contains an array of four S1 objects: 例如,此struct S2包含四个S1对象的数组:

struct S1 { int a; };

struct S2
{
    S1 the_array[4];
};

Sure, why not. 当然,为什么不呢。

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

baz.bar[1].a = 5;

Yes, structs can contain other structs. 是的,结构可以包含其他结构。 For example: 例如:

struct sample {
  int i;
  char c;
};

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

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

相关问题 C ++中的结构中的模板问题 - Issue with template in structs struct in c++ 如何在C ++中制作结构类型数组 - How to make a struct type array in C++ 可以在C ++中制作单例结构吗? 怎么样? - Possible to make a singleton struct in C++? How? C ++和Swift:如何在C ++堆栈帧中处理结构? 结构继承的复杂性是什么Swift不支持结构继承? - C++ and Swift: How are structs handled in C++ stack frames? Are the complications of struct inheritance why Swift does not support struct inheritance? 当作为与结构相同类型的参数传递时,包装在 C++ 类中的 C 结构如何交互? - How do C structs wrapped in C++ classes interact when passed as a parameter of the same type as the struct? 如何使用指向数组的指针访问结构数组中包含的结构中的字段? [C ++] - How to access a field in a struct contained in an array of structs using a pointer to that array? [C++] 在 C++ 中,如何为其中包含结构数组的结构创建 constexpr 聚合初始化? - In C++, how do I create a constexpr aggregate initialization for a struct that has an array of structs in it? 无法在C ++中制作结构向量 - Cannot make a vector of structs in C++ 使用匹配的字符串搜索结构的c ++ std向量 - Searching c++ std vector of structs for struct with matching string C ++:为什么我的结构向量充当一个结构? - C++: Why is my vector of structs acting as one struct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM