Skip to main content

Structs with Structs

The world organizes data in complex ways. This means that we often want to organize our information in ways that mimic this organization.

Consider the previous example where we created an array of Students. This array of Students represent:

  • Each student has the student id, their midterm and final grades
  • We have an array of all students for a class.

This is a fine representation if we only cared about one particular section of a course... what if I wanted to know about the midterm and final marks for every section of a course? Our array of students would not be sufficient to do this... we would want more information.

Suppose that we want to represent the folloing for each class:

  • course code (Example: IPC144)
  • section code (Example: NAA)
  • professor (first and last name)
  • number of students
  • list of all the students and their grades (array of student structs)

How would we organize data to do all of this?... We can create structs with data members are also structs. Doing this simply extends what we already know and apply the same syntactic rules to our data.

Let us consider each piece of information that we wish to store:

  • course code (Example: IPC144) - this is just a null terminated string.
  • section code (Example: NAA) - this is also a null terminated string
  • professor (first and last name) - we will create a professor struct and have an instance of that struct as a data member.
  • number of students - an integer
  • list of all the students and their grades (array of StudentScore structs)

struct Definitions:

The Professor struct. We are adding this struct to represent a professor. By putting it in a struct:

#define MAXNAME 50
struct Professor
{
char firstName[MAXNAME+1];
char lastName[MAXNAME+1];
};

Recall that our StudentScore struct from the previous example was:

struct StudentScore
{
int studentNumber;
float midterm;
float final;
};

Putting all of this together we can create a Section struct as follows:

struct Section
{
char courseCode[7]; //6 chars + 1 null char
char section[4]; //3 chars + 1 null char
struct Professor prof; //an instance of the Professor struct
int numStudents; //number of students
struct StudentScore students[40]; //all the students and their scores
};

Initialization of a Struct of Structs

Remember that the syntactic rules of using complex structs is to apply the same rules as would a regular struct.

The basic syntactic rule for initializing arrays and structs are as follows: _ data members are initialized in a list determined by the order of definition data members. _ arrays are also initialized one element at a time in the list.

All we need to do to initialize the members of this is to expand out each component. Below is how to think about the syntax

//Section struct has 5 data members, so our initializer has 5 items in a list separated by
//comma's. Each data member's initial value (currently represented as blanks)
//is separated by a comma
struct Section mysection = { ___, ___, ___, ___, ___ };

Now, lets initialize this with "IPC144", "NAA", and number of students (3, we will use the same data as our previous example)

struct Section mysection = { "IPC144", "NAA", ___, 3, ___ };

Next, we want to intialize the prof data member. Let's say that our professor was named Mustrum Ridcully. If we were to initialize an instance of Professor we would do this:

    struct Professor prof = {"Mustrum", "Ridcully"};

Thus, we can extend this to how we would initialize the prof data member by putting that initialization for the third data member:

    struct Section mysection = { "IPC144", "NAA", {"Mustrum", "Ridcully"}, 3, ___ };

Similarly recall that we had previously initialized an array of student structs in this manner:

struct StudentScore students[40]={
{123456, 63.5, 65.0},
{654321, 70.5, 75.3},
{345678, 91.5, 82.3}
};

We can apply this initialization to the 5th data member of this struct. each member has been placed on a separate line for readability but syntactically, it is allowed to be on the same line.

struct Section mysection = {
"IPC144", //data member 1 the course code
"NAA", //data member 2 the section
{"Mustrum", "Ridcully"}, //data member 3 a Professor Struct
3, //data member 4 the number of students
{ //data member 5, is this and next 4 lines
{123456, 63.5, 65.0}, //each line is an element of the array, each
{654321, 70.5, 75.3}, //element is a StudentScore Struct
{345678, 91.5, 82.3}
},
};

Accessing a struct declared inside a struct.

To access a data member of a struct that is part of another struct we work our way in from our variable into our data member.

For example, if I wanted to access the Last name of the professor in the previous example I would write:

mysection.prof.lastName

Similarly we can access the midterm mark of the 1st student using:

mysection.students[0].midterm