1

What is the main benefit of putting a single array inside a struct? When would you do it? As an example:

struct Database{
  struct Address rows[MAX_ROWS];
};

Now to create a database:

struct Database* db = malloc(sizeof(struct Database));

However it could be done like this too:

struct Address* db = malloc(sizeof(struct Address) * MAX_ROWS);

I think one reason is better documentation/understanding from code (code self explanation), for example the signature foo(struct Database*) seems much better than foo(struct Address*), but is there another important reason too? Is there a general rule for when you would wrap an array within a struct?

aderchox
  • 3,163
  • 2
  • 28
  • 37

0 Answers0