Date: agosto 25, 2025
Author: Guillermo Garcia
Categories: Embedded C programming Tags: Embedded C programming
In this article, we’ll explore structures and unions, one of the most efficient ways to manage variables of different types as organized data in embedded system programs.
Table of Contents
In the articule Array in C programming the concept of arrays was introduced. These arrays are used to combine different variables into 1. Unfortunately, all variables in an array need to be of the same datatype. In real life, however, we often encounter a group of variables of different datatypes that belong together. Such a group of variables is called a record. To represent this real life situation, structures will be used.
A structure combines a set of variables of different datatypes into 1 variable.
A structure must be defined using the struct statement as follows :
struct <name>{
datatype elem1;
datatype elem2;
...
};
The statement above defines a new data type. Variables of that new datatype can be declared by:
struct <name> <variable_name>;
For instance, the definition of the structure product looks like:
struct product{
int number;
char description[30];
int inventory;
float purchase;
float retail;
};
and the declaration of variables of this new datatype is done as follows:
struct product art1, art2;
or an array of variables of this new datatype:
struct product art[100];
Once the structure is defined and variables of the new datatype are declared, the different members of those variables need to be accessible. To access any member of a structure, the dot operator (.) is used. This dot operator is placed in between the variable name and the structure member
name.
Example:
art1.number = 12; strcpy(art[20].description, “plasma TV”);
The individual structure members can be used like any other variable of that same type as can be seen in the examples above.
Also the structure variables themselves are regarded as ordinary variables. As a result, a structure variable can be used directly in an assignment which is not the case for array variables:
art[0] = art1;
Initialization of a structure variable can also be done together with the declaration as follows:
Product art1={12, “plasma TV”, 35, 245.50, 999.99};
Next to variables and arrays, structures can also contain other structures. The following example shows how nested structures can be used.
#include<stdio.h>
struct Date
{
unsigned short day;
char month[16];
unsigned int year;
};
struct Person
{
char name[32];
struct Date DateOfBirth;
};
int main(void)
{
struct Person p = { "Mark Twain", { 7, "February", 1871 } };
printf("name : %s\n", p.name);
printf("date of birth: %s, %u, %u\n", p.DateOfBirth.month,
p.DateOfBirth.day, p.DateOfBirth.year);
return 0;
}
Note that the structure “Date” needs to be defined before it can be used in the structure “Person”.
Individual structure members as well as entire structures can be passed to a function as argument or returned from a function as function return value.
When an individual structure member or an entire structure is passed to a function as argument, it is passed by value, meaning that the structure value in the calling function cannot be modified from within the called function. The modifications done inside the called function can be returned to the calling function by the use of a function return value.
Product adapt(Product x)
{
x.number += 1000;
x.retail *= 1.2;
return x;
}
Calling the function can be done as follows:
art[5] = adapt(art1);
Like for any other variable that is passed by value, the values of the structure members of art1 are copied into the variable x which acts as a local variable in the function adapt. The return statement takes care of
copying all members of x into art[5].
C does not provide an equality operator that allows to directly compare 2 structures. Comparison can only be done by comparing the structure variables member by member.
The following example shows a function that can be used to compare two structures of the type Product:
int compare(Product x, Product y)
{
if(x.number != y.number) return 0;
if(x.inventory != y.inventory) return 0;
if(x.purchase != y.purchase) return 0;
if(x.retail != y.retail) return 0;
return !strcmp(x.description, y.description);
}
Like for any other data type, pointers to structures can be declared as shown in following example:
Product a; Product *pa; pa = &a;
Where pa is declared to be a pointer to a variable of the type Product. The statement “pa=&a;” assigns the address of the structure variable a to the pointer pa.
To access the individual structure members via the pointer variable, the ->
operator needs to be used:
pa->number = 1023; strcpy(pa->description, “dvd”);
Pointers to structures also allow to pass a structure to a function by reference.
If one space in memory is requested to store a variety of data types, unions can be used to accomplish this task. A union is a special data type available in C that allows to store different data types in the same
memory location. You can define a union with many members, but only one member can contain a value
at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose. The syntax can be seen from the following example, initialize the union Data to store an integer, a floating point number, and a character string:
union <name>{
datatype elem1;
datatype elem2;
...
};
The statement above defines a new data type. Variables of that new datatype can be declared by:
union <name> <variable_name>;
union Data{
int i;
float f;
char str[20];
};
The memory occupied by a union will be large enough to hold the largest member of the union. For
example, in the above example, Data type will occupy 20 bytes of memory space because this is the
maximum space which can be occupied by a character string. To store this union as any of these data types, the following type of code could be used:
union Data{
int i;
float f;
char str[20];
};
union Data data;
data.i = 10;
data.f = 220.5;
strcpy(data.str,"C programming");
A union in C is similar to a structure, except that all members start at the same memory location.
A union variable can represent the value of only one of its members at a time.
As a special note, in a union, the memory consumed by the union is equal to the largest member.
These concepts are important for embedded systems because together Structures and Unions we can use unions, structures, and bit fields to store data in contiguous sections of memory.

Deja una respuesta