Maria Montero

Learn the embedded C programming language: understand the object of …

Learn about data objects called unions in embedded C language.

The difference between structure and union in embedded C

In a previous article in this series, we discussed that embedded C structures allow us to group variables of different data types and treat them as a single data object.

In addition to structures, the C language supports another data construction, called union, which can group different types of data as a single data object. This article will provide basic information about unions. We will first look at an introductory example of declaring a union, then we will examine an important application of this data object.

Introductory example

Declaring a union is very similar to declaring a structure. We just need to replace the keyword “struct” with “union”. Consider the following sample code:

                    prueba sindical {
uint8_t c;
uint32_t i;
};
                  

This specifies a template that has two members: “c”, which takes one byte, and “i”, which takes four bytes.

Now, we can create a variable from this union template:

                    prueba sindical u1;
                  

Using the member operator (.), We can access the members of the union “u1”. For example, the following code assigns 10 to the second member of the union above and copies the value of “c” to the variable “m” (which must be of type uint8_t).

                    u1.i = 10;
m = u1.c;
                  

How much memory space will be allocated to store the variable “u1”? While the size of a structure is at least as large as the sum of the sizes of its members, the size of a union is equal to the size of its largest variable. The memory space allocated to a union will be shared among all union members. In the example above, the size of “u1” is equal to the size of uint32_t, that is, four bytes. This memory space is shared between “i” and “c”. Therefore, assigning a value to one of these two members will change the value of the other member.

You may be wondering, “What is the point of using the same memory space to store multiple variables? Is there an application for this feature?” We will explore this problem in the next section.

Do we need shared memory space?

Let’s look at an example where a union can be a useful data object. Suppose, as shown in Figure 1 below, there are two devices in your system that need to communicate with each other.

Figure 1

“Device A” must send status, speed and position information to “Device B”. Status information consists of three variables that indicate battery charge, operating mode, and ambient temperature. The position is represented by two variables that show the positions of the x and y axes. Finally, speed is represented by a single variable. Assume that the size of these variables is as shown in the following table.

Variable name Size (Byte) Explanation
power 1 Charged battery
operation mode 1 Operation mode
temperature 1 Temperature
x_pos 2 X position
y_pos 2 Y position
vel 2 Speed

If “Device B” constantly needs to have every piece of this information, we can store all these variables in a structure and send the structure to “Device B”. The size of the structure will be at least as large as the sum of the size of these variables, that is, nine bytes.

Therefore, every time “Device A” talks to “Device B”, it needs to transfer a 9-byte data frame over the communication link between the two devices. Figure 2 shows the structure that “Device A” uses to store the variables and the data frame that needs to go through the communication link.

Figure 2

However, let’s consider a different scenario where we only occasionally need to send the status information. Also, suppose that it is not necessary to have position and velocity information at any given time. In other words, sometimes we only send position, sometimes we only send speed, and sometimes we only send status information. In this situation, it does not seem like a good idea to store the information in a nine-byte structure and transfer it over the communication link.