How to Create and Initialize a Structure in C

Embedded C for microcontrollers



In this article, we go over how to create and initialize a structure in C.

A structure is a data element in C that can be composed of elements of different data types. It is used to create user-defined data types and allows us to combine data of different types.

As an example, let's go over creating a data structure representing a house.

We will have data elements, including the house size, house price, number of bedrooms, number of bathrooms, and whether the house has a pool or not.

These data elements will be of different types. The house size will be of a 32-bit integer. The house price will be of a 32-bit integer size. The number of bedrooms will be an 8-bit integer. The number of bathrooms will be an 8-bit integer. The presence of a pool will be a boolean variable for true or false.

So you see we have many different data types present within this structure including a 32-bit integer, a 16-bit inteer, 8-bit integers, and a boolean value. This is the dynamic nature of structures, allowing us to combine different data types.

Data structures allow us to represent a larger data we are describing such as a house and it allows for maximum efficiency and description.

For example, the number of bedrooms would not have to be a 32-bit integer, because a house is not going to have more than 256 bedrooms, which is the maximum value an unsigned 8-bit integer can hold. Therefore, it would be inefficient to use a 32-bit integer, since it would represent a waste of space. The same is true for the number of bathrooms. However, the house price must be a 32-bit number as the number is usually over 200000. The square footage must be a 16-bit number because it can be up to 65536. 8-bit would be too small and 32-bit would be too large.

So below we create a structure and initialize it with the example above.





So we create a structure data type called House.

This is composed of several elements including housePrice (uint32_t), houseSize (uint16_t), houseBedrooms (uint8_t), houseBathrooms, and pool (bool).

The struct data type is normally declared outside of the main function.

The struct data type can even be declared within a header file.

The struct data type is never really declared within the main function.

We then have our main function.

This is the place, however, where we initialize the struct data type.

There are 2 ways we can initialize the struct data type.

Above, we use a period before the name of each struct data element and set it equal to its value.

Another way is you can omit the period and the name and specify each value but it must be done in the correct order. This will be shown below.

We create a struct element that is of type House; we name it redHouse.

The house size is 1200 (square feet), the house price is $250000, the number of bedroom is 2, the number of bathroom is 2, and there is no pool, so we set the value equal to false.

We then output the redHouse data elements using the printf statements.

The output of this program is shown below.



The other way we can initialize a struct data type with the same data above is shown below.



In this example, we initialize the redHouse struct data type using the corresponding values according to the order they are declared in the original definition of the struct data type.

This means that the first value specified will be for the house price, the second value is for the house size, the third value for the number of bedrooms, the fourth value is for the number of bathrooms, and the fifth value is for whether or not a pool is present.

We get the same exact output as shown above.



So this is how we can create and initialize a structure in C.

Related Resources

How to Set Bits of a Number in C

How to Clear Bits of a Number in C



HTML Comment Box is loading comments...