Tutorial 6 – Structs

In the last tutorial we saw how we could simplify our code and do more complex things by using functions. C has yet more benefits that we can take advantage of. Lets say we want to model a real life situation, for example we want to store some information on a group of people. We could do that with our old code but we’d need to create a new variable for each piece of data we want to store on each person e.g:

This isn’t terrible but there are better solutions. Lets take a look at structs. Here is an example of how to use structs:

struct structName
{
int variable1;
float variable2;
};

The above code says that there is a type of structure called “structName” which contains the 2 variables named within the brackets. Here is an example of a struct that contains a person’s data and how we would go about accessing that data: