The STRUCT are data structures that allow grouping variables of different types into a single entity.
If we said that variables are like boxes, STRUCTS are like a lunchbox or a mixed plate. They are containers that have other boxes inside (so that the peas don’t mix with the rice).
- It has other departments inside
- Each department has the necessary size
- Together they form a single container
For example, imagine you need to store data about people. For the example, a person will have a name, date of birth, and email. It makes sense to have a grouping of variables that allows me to work with them together.
- stringdni
- datedateOfBirth
- stringemail
That is precisely a STRUCT, a grouping of variables that we create to work with them at the same time. This way, it is much more convenient than having the variables loose.
The STRUCT are useful when you need to group related variables, but you do not require the complexity and flexibility of objects.
Unlike objects, STRUCT are generally used to group simple data. Additionally, they lack associated functionalities or methods, they only contain data.
Examples of Structs in different languages
Almost all programming languages have some type of variable grouping, which can be called Struct or other names but, basically, it is the same.
Logically, the syntax will depend on the programming language we are using. Let’s see examples in some popular languages,
struct Person
{
public string Name;
public int Age;
public double Height;
}
// Accessing elements of a structure
Person person;
person.Name = "Luis";
person.Age = 25;
var height = person.Height;
struct Person
{
std::string Name;
int Age;
double Height;
};
// Accessing elements of a structure
Person person;
person.Name = "Luis";
person.Age = 25;
double height = person.Height;
const person = {
name: "Luis",
age: 25,
height: 1.75
};
// Accessing elements of a structure
person.name = "Luis";
person.age = 25;
let height = person.height;
class Person:
def __init__(self, name, age, height):
self.name = name
self.age = age
self.height = height
# Accessing elements of a structure
person = Person("Luis", 25, 1.75)
In the previous examples, we created structures that represent information about a person, including their name, age, and height.
The syntax varies slightly between languages, but the concept is the same: defining an entity that groups related data.
Internal Functioning Advanced
Internally, in most programming languages, a STRUCT is stored in memory in a contiguous manner. When an instance of a STRUCT is created, the system allocates a block of memory that can contain all its members.
For example, if you have a STRUCT called Person with members for the name, date of birth, and email, memory will be reserved to store this data consecutively, in the order they were declared.
When you access a member of a STRUCT, the system can directly calculate its position in memory by adding an offset from the base address of the STRUCT.
Since the members of a STRUCT are stored contiguously, accessing this data is very efficient. It is simply a memory access operation with an offset.
On the other hand, in many languages, STRUCT are value types (not references). This means that a copy of the STRUCT is created and that copy is passed to the function. Depending on the size of the STRUCT, this can have implications for performance and the amount of memory used.
Therefore, it is advisable to know the implementation details in your particular language. But don’t worry too much, in most languages the compiler makes decisions and can treat references as values, or vice versa, if it understands that it is more efficient.