Structured Data Types in C For Dummies By a Dummy

Anush Somasundaram
4 min readAug 17, 2021
Image sourced from musthavemenus.com

All restaurants have menus and all menus mostly have the same layout. You’ve got your starters section, the soup section, the main course section , the desert section and the drinks section. Now some menus can have other sections apart from those but that doesn’t matter to us. What we have sort of done now is break down menus and noticed that there is a pattern. Now suppose I wanted to build a program that holds the menus of various restaurants in the Upper East Side, it would be tedious to assign variables to each and every category in the menu and it would truly be a horror if I had to do it for each one of the restaurants in the area. There are a million places where I can go wrong and even if I do manage to have all the variables correctly initialised and assign values to each of them, retrieving them would be a pain.

This is where structured data types come in to save us, a structure will let you rap all the little pieces of data from a menu into one brand spanking new data type, and all of a sudden half your problems are resolved. Structures allow us to hold variables of not only one data type but of multiple. So a structure can hold an integer type, string type, float type and any data type you can think of. They can hold how many ever variables you want them to hold. In our case we need to store the list of the food on the menu in sections, why not create a structured data type called menus.

We can create structs by using the struct keyword. Go through the code snippet below.

screenshot of code snippet

We first use the keyword “struct” followed by what you’re going to name that particular structure.

Then we specify all the variables we require inside parentheses and after the close parentheses we add a semicolon to let the compiler know that it is the end of the struct. Notice there is also another keyword “typedef”, typedef allows the programmer to properly name the structure as a data type and not use the syntax “struct (name of struct)” every time he creates a new record.

Now we shall create a record using the menus struct.

We have created a record with the starters, main course and desert options for a restaurant named Sfoglia on the Upper East Side. Now we can access these pieces of data by using the syntax “record_name.variable_required”.

It’s done as shown below.

The output of those three lines is shown below.

As we can see the data in the structure has been returned to us. We can create the same record for multiple restaurants by giving the records different names. This makes storing and retrieving data a lot more efficient and easy. We can pass the data inside the structures into functions and work with them. Structures just bring a little structure into the program, no pun intended.

Just for the sake of it let’s create another record of the menu of a different restaurant called JoJo.

We can retrieve the starters at JoJo just like we retrieved the starter at Sfoglia.

As you would expect we get the output given below.

So now we can compare the starters at JoJo and at Sfoglia. Not that it actually makes a difference, but we can. Hence this is how structures can be created and used in C. Hope this was helpful.

--

--

Anush Somasundaram

Looking for interesting software projects (ML/DL/NLP anything).