Microcontroller Embedded C Programming Lecture 144| Introduction to structures

  • Post author:
  • Post category:Blog

 

Structures in C

 

Structures play a crucial role in ‘C’ programming, enabling the creation of user-defined data types that combine various data elements.

These data elements can belong to different data types, allowing for more complex and organized data structures.

 

How to create a structures?

A structure is nothing but a record, it’s a record where we club data of different data types. 

 

Syntax of structure

Figure 1. Creation of structure
Figure 1. Creation of structure

→ Begin with the keyword ‘struct', a fundamental reserved term in ‘C’ programming.

→ Specify a meaningful ‘tag_name' to identify the structure. You have to give one name for your record or you have to give a name for your structure.

→ Enclose the structure definition within curly braces ‘{}'. Remember to conclude the definition with a semicolon ‘;'.

After that, inside the body, you can mention ‘n’  number of member elements.

This is a syntax to create a structure in ‘C’.

 

Example of a Structure

Structures in C Programming
Figure 2. Example of a Structure

Consider the structure CarModel, which contains member elements like carNumber, carPrice, carMaxSpeed, and carWeight So, the member elements usually describe the record. 

For example, in the structure CarModel, the member element describes the car number, car price, car maximum speed, and car rate. All of these are different data of different data types. So, by using a structure you can combine all those data of different data types in a single record.

You should remember that structure definition doesn’t consume any memory. It is just a description or a record. 

 

 

Variables of a structure

 

How to create a variable?

To create a variable you have to first mention the data type followed by the variable name. So, the same thing you have to use here. 

struct CarModel CarBMW, CarFord, CarHonda;

The creation of structure variables is shown in Figure 3.

Structures in C Programming
Figure 3. Variables of a structure

 

First, you have to mention the data type. In this case, struct CarModel is a data type. That’s a user-defined data type. And then you have to mention various variables. Here, we have created 3 variables: CarBMW, CarFord, and CatHonda. Memory will be consumed when you create structure variables.

 

For example, here you can calculate how much memory a CarBMW variable will consume.

If you want to calculate this, then you have to calculate the total memory consumed by the member elements of that structure.  

Assume carNumber consumes 4 bytes, carPrice consumes 4 bytes, carMaxSpeed consumes 2 bytes, and carWeight consumes 4 bytes. Total 14 bytes. So, a single variable CarBMW consumes 14 bytes of memory. It’s like that.

What is the data type of CarBMW?

The data type of the CarBMW variable is “struct CarModel”.  That’s a user-defined data type. 

 

 

Structure member element initialization

Structures in C Programming
Figure 4. Structure member element initialization

We will understand this by going through one example. 

 

Write a program to create a carModel structure discussed and create two variables of type carModel. Initialize the variables with the below given data and then print them. 

  1. 2021, 15000, 220, 1330
  2. 4031, 35000, 160, 1900.96

We’ll do this exercise in the following article.

 

Get Microcontroller Embedded C Programming Full Course Here.

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.