What about nested structs?

There is also the ability to make a nested struct. One way is to declare a struct then use that struct inside another struct. For example:

struct date {
    int day;
    int month;
    int year
};
struct bookinfo {
    char title[50];
    char author[50];
    char subject[100];
    char isbn[11];
    struct date pubdate;
};

Then assigning a value to the year would occur as follows:

struct bookinfo b;
b.pubdate.year = 2015;

The second way is embedding a struct within a struct:

struct bookinfo {
    char title[50];
    char author[50];
    char subject[100];
    char isbn[11];
    struct date {
        int day;
        int month;
        int year;
    } pubdate;
};

Which works in the same way.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s