Contents

What is a struct?

It is common that we write programs that have to track things like people, packages, pets, etc. For instance, say we are creating a program that keeps track of student grades in a class. For each student, there are several pieces of information we would like to track: their name, student id, and overall grade. If we are tracking a single student, that's simple—we just store each piece of information in a variable, for a total of three variables. Here's what it might look like:

Now what if we want to track a class of 10 students? We need to declare three variables per student—that's 30 variables! Here's what it looks like for the first few students...

Note that we've prepended studentN for N from 1 to 10. This is clearly tedious and error prone. What would be great is if we could have one variable per student, and have that variable somehow know about all of the pieces of information we want. In C++, one way we can do this is using structs. A struct is a C++ construct that allows us to bundle a set of variables (we call these fields or data members) and give it a name. That name is used exactly like a variable type (e.g., int or string). So, we can then declare a new variable of this type and access all of the fields. We'll look at each of these phases in detail.

(Back to top)

Defining a struct

The first thing we need to do in using a struct is to define one. We do this below our header, and above main. The format is: the keyword struct followed by the name of the struct (start with a capital letter to make it obvious that its a type and not a variable), followed by a pair of curly braces and a semi colon. We then declare any variables we need inside of the curly braces. Here's what a struct definition might look like to hold a single student's data. We're calling it Student:

That's all there is to defining a struct.

(Back to top)

Creating an instance of a struct

Now that we've defined a struct, we need to create an instance of it. We can use the name of a struct in the exact same way we use a type. Building on the example from above, we can create a variable that is of type Student. Here's what it looks like in code for the first student:

This create a new instance of the struct, so student1 in this example is now has it's own copy of each of the fields of the Student struct. However, we need to set each of the fields, which we'll see how to do next.

(Back to top)

Accessing the fields of a struct instance

Once an instance of a struct has been create, we can access a fields by using the notation: variable.fieldName. That is, the variable name, followed by a dot ("."), followed by the field name. To access the name field for the student1 instance, we write: student1.name. We can use that expression just like a variable: we can assign values to the field, print the field, or use it in expressions. Here's how we would initialize the fields for the first student:

Note that we don't need to specify the type of each of the fields for our instance. Why not? Because we already did that in our struct definition. Here's what it looks like when we create instances of Student for a few more of the students:

Now, it may seem like we've actually added additional code. In this example, we did. However, we only have one explicitly declared variable per student; the three variables for the student information are only defined once at the beginning, so we don't need to redefine them each time. When we get to functions, we will see some ways in which we can further limit our code, so we don't need three lines per student to initialize each of the struct fields.

(Back to top)