How to create a structure structure in C ++

Can a structure contain other structures?

I would like to create a structure containing an array of four other structures. Is it possible? What does the code look like?

+3
source share
3 answers

Yes, you can. For example, this structure S2contains an array of four objects S1:

struct S1 { int a; };

struct S2
{
    S1 the_array[4];
};
+8
source

Of course, why not.

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

baz.bar[1].a = 5;
+4
source

Yes, structures can contain other structures. For instance:

struct sample {
  int i;
  char c;
};

struct b {
  struct sample first;
  struct sample second;
};
+2
source

Source: https://habr.com/ru/post/1746693/


All Articles