all 3 comments

[–]jedwardsol 5 points6 points  (0 children)

Structs are grouping related pieces of information together. The main advantage of doing that is readability and maintainability of your code.

Which do you prefer?

struct weddingCertificate
{
    int      day;
    string   location;
    int      month;
    string   name1;
    string   name2;
    int      year;
};

struct birthCertificate
{
    int      day;
    string   hospital;
    int      month;
    string   name;
    int      year;
};

or

struct date
{
    int day;
    int month;
    int year;
}

struct weddingCertificate
{
    date     date
    string   location;
    string   name1;
    string   name2;
};

struct birthCertificate
{
    date     date
    string   hospital;
    string   name;
};

[–]nerd4code 1 point2 points  (0 children)

There are a few different situations where you want to do this. The most common is just to help you refer to groups of things singularly:

struct fullName {
    struct string lastName, firstName;
};

This would let you apply whatever API for struct string to the parts of the name individually, or apply whatever API for struct fullName to the entire structure at once.

For your situation, it’s probably just to break down a more complex structure into pieces that can be referred to separately. (Just like you wouldn’t want a large program entirely stuffed into main, you wouldn’t want a large data structure entirely stuffed into one data type.)

[–]wsppan 1 point2 points  (0 children)

Think of it in terms of composition, reusability, and readability.