Microcontroller Embedded C Programming Lecture 145| Accessing structure member elements

  • Post author:
  • Post category:Blog

 

Accessing structure member elements

 

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

 

The CarModel structure you have to create is shown in Figure 1.

Figure 1.Structure member element initialization
Figure 1.Structure member element initialization

 

Let’s see how to create this in our program.

In the project, first I created the structure. Usually, structure definition is created outside the function.  So, you can even create inside the function, no problem with that. But it is usually done outside the program or in the header file. It’s better if you create the structure definition in the header file.

Figure 2. Code
Figure 2. Code

 

Let’s first create here itself in the main.c.

Write the ‘struct‘ keyword and let’s give the tag_name CarModel.

After that open the curly brackets and create the member elements. Member elements are carNumber, carPrice, carMaxSpeed, and carWeight. I created the member elements of the CarModel structure(lines 7 to 13). 

 

After that let’s create variables. You can create either local variables or global variables. For this, we can create local variables. 

How to create a variable? 

To create a variable you have to first mention the data type followed by the variable name.

Here, struct carModel is a data type. After that, I type carBMW, carFord, and carHonda. These are the variables.  I initialize the variables by using the data given in the exercise. 

2021, 15000, 220, 1330 are the data you have to use for variable1, and 4031, 35000,160, 1900.96 the data you have to use for variable2.

Now let’s start with variable carBMW.

Here you can initialize this variable using an assignment operator is equal to(=), and then you have to start mentioning the values for these member elements inside the curly bracket. I mentioned 2021, 15000, 220, 1330 (line 17).

Remember that, there is a one-to-one mapping between the order in which you have used the values and the order in which the member elements appear.  For example, here 2021 will be stored in car number, 15000 will be stored in car price, and 220 will be stored in car maximum speed, like that. So, the order is important here.

Let’s create the second variable is carFord and let’s use the values given in the exercise(line 18).

We just created two variables of the structure and initialized them. So, we just tried the C89 method. This method was introduced in C89 and here the order is important.  After that C99 standard was introduced. Here the order is not important, but you have to use designated initializers using the dot(.) operator, as shown in Figure 1. 

Let’s try to use the C99 method to initialize the structure variable.

I created one more variable here. 

struct carModel carHonda = {.carWeight = 90.90, .carPrice = 15000};

Here, I can initialize the member element first. To do that, you have to use a dot operator “.” (dot), then you have to mention the member element name. This method is also valid.

Accessing structure member elements in C is done using the dot . operator. When you create a structure variable, you can use the dot operator to access and modify its individual member elements.

 

Here, the order is not important. You just use the dot operator and use the member element which you want to initialize. You can give a comma, and then give the dot operator, and then press control space, eclipse will give some suggestions about the member elements, then you can select whatever you want. carPrice = 15000. (line 19)

 

Let’s print each and every member elements of the carBMW structure. For that, we have to understand accessing structure members.

 

Accessing Structure members

When a structure variable is created, use a “.”( dot) operator to access the member elements. 

Let’s use the dot operator. 

First, use a printf statement. Printf(”Details of a car BMW is as follows\n”);

Then print carNumber. So, printf(“carNumber = %u\n”,carBMW.carNumber);

How to access the member element carNumber?

For that, first, you have to use the structure variable, then give the dot operator, and after that select the member element carNumber. That’s how you access the member element of a structure variable.  Like that, finish this for other member elements, as shown in Figure 2.

If you want to change the value of member elements of some structure variables, let’s say you want to overwrite the initialized values, you can do that by again assigning new values to the member elements of the structure variable.

Let’s say, if I want to change the value of a carNumber of carFord, I can do that. So, just mention the member element and after that just mention the structure variable, then select the member element let’s say carNumber, and then you can assign some different value.

carFord.carNumber = 1000; // This is called assigning a new value to the member element of a structure variable. 

 

Get Full Course on Microcontroller Embedded C Programming 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.